feat: chartTabs组件的状态存储由window改为store

This commit is contained in:
刘洪洪
2023-01-13 15:03:45 +08:00
parent 089887f05b
commit 0f2fcbe9e6
2 changed files with 30 additions and 15 deletions

View File

@@ -45,6 +45,7 @@
import { overwriteUrl, urlParamsHandler } from '@/utils/tools' import { overwriteUrl, urlParamsHandler } from '@/utils/tools'
import { ref } from 'vue' import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router' import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
export default { export default {
name: 'ChartTabs', name: 'ChartTabs',
@@ -69,8 +70,9 @@ export default {
setup (props) { setup (props) {
const tabsData = ref([]) const tabsData = ref([])
const router = useRouter() const router = useRouter()
const store = useStore()
const routerPath = router.currentRoute.value.path const routerPath = router.currentRoute.value.path
const tabList = window.currentChartTabList const tabList = store.getters.getChartTabList
let currentTab = '0' let currentTab = '0'
if (props.data) { if (props.data) {
@@ -94,7 +96,7 @@ export default {
return item.path === routerPath return item.path === routerPath
}) })
currentTab = JSON.stringify(currentTab) currentTab = JSON.stringify(currentTab)
window.currentChartTabList = [{ path: routerPath, index: currentTab }] store.dispatch('dispatchChartTabList', [{ path: routerPath, index: currentTab }])
} else { } else {
// 此处为切换界面如果window里保存的路径和tabsData里的路径一致选择window数据并使用 // 此处为切换界面如果window里保存的路径和tabsData里的路径一致选择window数据并使用
// 两个不一致的话则默认选择tabsData里的第一条 // 两个不一致的话则默认选择tabsData里的第一条
@@ -111,12 +113,12 @@ export default {
// 场景从遮罩面板进入界面时重置状态默认选中第一个tab // 场景从遮罩面板进入界面时重置状态默认选中第一个tab
if (routerPath === tabList[0].path) { if (routerPath === tabList[0].path) {
currentTab = tabList[0].index currentTab = tabList[0].index
window.currentChartTabList.splice(1, 1) store.dispatch('dispatchChartTabList', [{ path: tabsData.value[0].path, index: '0' }])
} }
} else if (obj0) { } else if (obj0) {
currentTab = tabList[0].index currentTab = tabList[0].index
} else { } else {
window.currentChartTabList = [{ path: tabsData.value[0].path, index: '0' }] store.dispatch('dispatchChartTabList', [{ path: tabsData.value[0].path, index: '0' }])
currentTab = '0' currentTab = '0'
} }
} }
@@ -161,9 +163,10 @@ export default {
} }
} }
}) })
const tabList = this.$store.getters.getChartTabList
if (window.currentChartTabList && this.router !== 'noRouter') { if (tabList && this.router !== 'noRouter') {
window.currentChartTabList.forEach((item) => { tabList.forEach((item) => {
this.$nextTick(() => { this.$nextTick(() => {
this.handleActiveBar(parseFloat(item.index)) this.handleActiveBar(parseFloat(item.index))
}) })
@@ -172,7 +175,7 @@ export default {
this.$nextTick(() => { this.$nextTick(() => {
this.handleActiveBar(this.currentTab) this.handleActiveBar(this.currentTab)
}) })
window.currentChartTabList = null this.$store.dispatch('dispatchChartTabList', null)
} }
}, },
handleActiveBar (index) { handleActiveBar (index) {
@@ -191,7 +194,7 @@ export default {
} else { } else {
// 数组长度为1即代表刚刷新界面获取active的dom添加样式避免原模式错位问题 // 数组长度为1即代表刚刷新界面获取active的dom添加样式避免原模式错位问题
// 可添加css样式也可添加class类名两个操作选一即可 // 可添加css样式也可添加class类名两个操作选一即可
if (window.currentChartTabList.length === 1) { if (this.$store.getters.getChartTabList.length === 1) {
// 此处操作是因为初始化时给active加border导致tab下移故需要将整体往上移动对应高度 // 此处操作是因为初始化时给active加border导致tab下移故需要将整体往上移动对应高度
const topDom = document.getElementsByClassName('el-tabs__header is-top') const topDom = document.getElementsByClassName('el-tabs__header is-top')
topDom[0].style.cssText += 'top: -3px' topDom[0].style.cssText += 'top: -3px'
@@ -206,6 +209,7 @@ export default {
}, },
handleClick (item) { handleClick (item) {
this.$emit('click', item) this.$emit('click', item)
const tabList = this.$store.getters.getChartTabList
if (this.router === 'noRouter') { if (this.router === 'noRouter') {
const { query } = this.$route const { query } = this.$route
@@ -215,14 +219,15 @@ export default {
}) })
overwriteUrl(newUrl) overwriteUrl(newUrl)
} else { } else {
window.currentChartTabList.push({ tabList.push({
path: this.tabsData[item.index].path, path: this.tabsData[item.index].path,
index: item.index index: item.index
}) })
if (window.currentChartTabList.length > 2) { if (tabList.length > 2) {
window.currentChartTabList.splice(0, 1) tabList.splice(0, 1)
} }
this.$store.dispatch('dispatchChartTabList', tabList)
this.$router.push({ this.$router.push({
path: this.tabsData[item.index].path, path: this.tabsData[item.index].path,
@@ -236,16 +241,16 @@ export default {
beforeUnmount () { beforeUnmount () {
clearTimeout(this.timer) clearTimeout(this.timer)
const path = this.$router.currentRoute.value.path const path = this.$router.currentRoute.value.path
const list = window.currentChartTabList const list = this.$store.getters.getChartTabList
if (list && this.router !== 'noRouter') { if (list && this.router !== 'noRouter') {
if (list[1]) { if (list[1]) {
// 去其他界面,清除状态 // 去其他界面,清除状态
if (path !== list[0].path && path !== list[1].path) { if (path !== list[0].path && path !== list[1].path) {
window.currentChartTabList = null this.$store.dispatch('dispatchChartTabList', null)
} }
} else if (path !== list[0].path) { } else if (path !== list[0].path) {
// 避免刷新页面之后又点击菜单栏进入该界面,还保留上次点击状态 // 避免刷新页面之后又点击菜单栏进入该界面,还保留上次点击状态
window.currentChartTabList = null this.$store.dispatch('dispatchChartTabList', null)
} }
} }
} }

View File

@@ -59,7 +59,8 @@ const panel = {
rangeEchartsData: {}, // 框选echarts图表 rangeEchartsData: {}, // 框选echarts图表
routerHistoryList: [], // 路由跳转记录列表 routerHistoryList: [], // 路由跳转记录列表
dnsQtypeMapData: [], dnsQtypeMapData: [],
dnsRcodeMapData: [] dnsRcodeMapData: [],
chartTabList: null // chartTabs组件的tab状态点击列表初始化为null方便原有逻辑计算
}, },
mutations: { mutations: {
setShowRightBox (state, flag) { setShowRightBox (state, flag) {
@@ -151,6 +152,9 @@ const panel = {
}, },
setRouterHistoryList (state, list) { setRouterHistoryList (state, list) {
state.routerHistoryList = list state.routerHistoryList = list
},
setChartTabList (state, list) {
state.chartTabList = list
} }
}, },
getters: { getters: {
@@ -225,6 +229,9 @@ const panel = {
}, },
getRouterHistoryList (state) { getRouterHistoryList (state) {
return state.routerHistoryList return state.routerHistoryList
},
getChartTabList (state) {
return state.chartTabList
} }
}, },
actions: { actions: {
@@ -253,6 +260,9 @@ const panel = {
}, },
clearPanel (store) { clearPanel (store) {
store.commit('cleanPanel') store.commit('cleanPanel')
},
dispatchChartTabList (store, list) {
store.commit('setChartTabList', list)
} }
} }
} }