【底座】路由参数保持

pull/249/head
俞宝山 2025-03-12 19:49:20 +08:00
parent f88765a34c
commit 68eed708be
3 changed files with 23 additions and 18 deletions

View File

@ -70,6 +70,7 @@
import routerUtil from '@/utils/routerUtil' import routerUtil from '@/utils/routerUtil'
import { useRoute, useRouter } from 'vue-router' import { useRoute, useRouter } from 'vue-router'
import { watch, ref, computed, nextTick, onMounted } from 'vue' import { watch, ref, computed, nextTick, onMounted } from 'vue'
import { useMenuStore } from '@/store/menu'
const route = useRoute() const route = useRoute()
const router = useRouter() const router = useRouter()
@ -83,6 +84,7 @@
const contextMenuTarget = ref(null) const contextMenuTarget = ref(null)
const contextMenuVisible = ref(false) const contextMenuVisible = ref(false)
const currentContextMenuTabIndex = ref(0) const currentContextMenuTabIndex = ref(0)
const menuStore = useMenuStore()
const viewTags = computed(() => { const viewTags = computed(() => {
return vStore.viewTags return vStore.viewTags
@ -100,7 +102,6 @@
}) })
watch(route, (to) => { watch(route, (to) => {
addViewTags(to) addViewTags(to)
activeKey.value = to.fullPath
}) })
watch(layoutTagsOpen, () => { watch(layoutTagsOpen, () => {
// closeOtherCacheTabs() // closeOtherCacheTabs()
@ -113,7 +114,8 @@
}) })
// tag // tag
const addViewTags = (to) => { const addViewTags = (to) => {
activeKey.value = to.fullPath const viewTag = viewTags.value.find((tag) => tag.path === to.path)
activeKey.value = viewTag ? viewTag.fullPath : to.fullPath
if (to.name && !to.meta.fullpage) { if (to.name && !to.meta.fullpage) {
vStore.pushViewTags(to) vStore.pushViewTags(to)
kStore.pushKeepLive(to.name) kStore.pushKeepLive(to.name)
@ -123,7 +125,6 @@
vStore.removeViewTags(firstTag) vStore.removeViewTags(firstTag)
} }
} }
// //
const treeFind = (tree, func) => { const treeFind = (tree, func) => {
for (const data of tree) { for (const data of tree) {

View File

@ -598,7 +598,9 @@
onSelectTag.value = true onSelectTag.value = true
const pathLength = obj.keyPath.length const pathLength = obj.keyPath.length
const path = obj.keyPath[pathLength - 1] const path = obj.keyPath[pathLength - 1]
router.push({ path }) const viewTags = viewTagsStore().viewTags
const viewTag = viewTags.find((item) => item.path === path)
router.push(viewTag ? viewTag.fullPath : path)
// //
selectedKeys.value = obj.selectedKeys selectedKeys.value = obj.selectedKeys
} }

View File

@ -9,10 +9,23 @@
* 6.若您的项目无法满足以上几点需要更多功能代码获取Snowy商业授权许可请在官网购买授权地址为 https://www.xiaonuo.vip * 6.若您的项目无法满足以上几点需要更多功能代码获取Snowy商业授权许可请在官网购买授权地址为 https://www.xiaonuo.vip
*/ */
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { useRouter } from 'vue-router'
export const viewTagsStore = defineStore('viewTags', () => { export const viewTagsStore = defineStore('viewTags', () => {
// 定义state // 定义state
const viewTags = ref([]) const viewTags = ref([])
const router = useRouter()
// 监听路由变化
watch(
() => router.currentRoute.value,
(newRoute) => {
const viewTag = viewTags.value.find((item) => item.path === newRoute.path)
if (viewTag) {
Object.assign(viewTag, newRoute)
}
},
{ immediate: true }
)
// 定义action // 定义action
const pushViewTags = (route) => { const pushViewTags = (route) => {
@ -22,12 +35,7 @@ export const viewTagsStore = defineStore('viewTags', () => {
viewTags.value.push(route) viewTags.value.push(route)
} }
if (target) { if (target) {
viewTags.value.forEach((item, index) => { updateViewTags(route)
if (item.path === route.path) {
viewTags.value[index] = { ...route, ...item }
// Object.assign(item, route)
}
})
} }
} }
const removeViewTags = (route) => { const removeViewTags = (route) => {
@ -39,9 +47,8 @@ export const viewTagsStore = defineStore('viewTags', () => {
} }
const updateViewTags = (route) => { const updateViewTags = (route) => {
viewTags.value.forEach((item, index) => { viewTags.value.forEach((item, index) => {
if (item.path === route.path) { if (item.fullPath === route.fullPath) {
viewTags.value[index] = { ...route, ...item } viewTags.value[index] = { ...route, ...item }
// Object.assign(item, route)
} }
}) })
} }
@ -56,12 +63,7 @@ export const viewTagsStore = defineStore('viewTags', () => {
viewTags.value.splice(index, 1) viewTags.value.splice(index, 1)
} else { } else {
// 路由存在,更新 // 路由存在,更新
viewTags.value = viewTags.value.map((item) => { updateViewTags(target)
if (item.path === target.path) {
return { ...item, meta: target.meta }
}
return item
})
} }
}) })
} }