From 68eed708be01c64d01a26ecbec47246b667e772d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BF=9E=E5=AE=9D=E5=B1=B1?= <1253070437@qq.com> Date: Wed, 12 Mar 2025 19:49:20 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=BA=95=E5=BA=A7=E3=80=91=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=8F=82=E6=95=B0=E4=BF=9D=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/layout/components/tags.vue | 7 +++-- snowy-admin-web/src/layout/index.vue | 4 ++- snowy-admin-web/src/store/viewTags.js | 30 ++++++++++--------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/snowy-admin-web/src/layout/components/tags.vue b/snowy-admin-web/src/layout/components/tags.vue index 88c9e4e1..912c3c19 100644 --- a/snowy-admin-web/src/layout/components/tags.vue +++ b/snowy-admin-web/src/layout/components/tags.vue @@ -70,6 +70,7 @@ import routerUtil from '@/utils/routerUtil' import { useRoute, useRouter } from 'vue-router' import { watch, ref, computed, nextTick, onMounted } from 'vue' + import { useMenuStore } from '@/store/menu' const route = useRoute() const router = useRouter() @@ -83,6 +84,7 @@ const contextMenuTarget = ref(null) const contextMenuVisible = ref(false) const currentContextMenuTabIndex = ref(0) + const menuStore = useMenuStore() const viewTags = computed(() => { return vStore.viewTags @@ -100,7 +102,6 @@ }) watch(route, (to) => { addViewTags(to) - activeKey.value = to.fullPath }) watch(layoutTagsOpen, () => { // closeOtherCacheTabs() @@ -113,7 +114,8 @@ }) // 增加tag 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) { vStore.pushViewTags(to) kStore.pushKeepLive(to.name) @@ -123,7 +125,6 @@ vStore.removeViewTags(firstTag) } } - // 查找树 const treeFind = (tree, func) => { for (const data of tree) { diff --git a/snowy-admin-web/src/layout/index.vue b/snowy-admin-web/src/layout/index.vue index 24c01574..17677ced 100644 --- a/snowy-admin-web/src/layout/index.vue +++ b/snowy-admin-web/src/layout/index.vue @@ -598,7 +598,9 @@ onSelectTag.value = true const pathLength = obj.keyPath.length 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 } diff --git a/snowy-admin-web/src/store/viewTags.js b/snowy-admin-web/src/store/viewTags.js index 1b2ea642..2b160540 100644 --- a/snowy-admin-web/src/store/viewTags.js +++ b/snowy-admin-web/src/store/viewTags.js @@ -9,10 +9,23 @@ * 6.若您的项目无法满足以上几点,需要更多功能代码,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip */ import { defineStore } from 'pinia' +import { useRouter } from 'vue-router' export const viewTagsStore = defineStore('viewTags', () => { // 定义state 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 const pushViewTags = (route) => { @@ -22,12 +35,7 @@ export const viewTagsStore = defineStore('viewTags', () => { viewTags.value.push(route) } if (target) { - viewTags.value.forEach((item, index) => { - if (item.path === route.path) { - viewTags.value[index] = { ...route, ...item } - // Object.assign(item, route) - } - }) + updateViewTags(route) } } const removeViewTags = (route) => { @@ -39,9 +47,8 @@ export const viewTagsStore = defineStore('viewTags', () => { } const updateViewTags = (route) => { viewTags.value.forEach((item, index) => { - if (item.path === route.path) { + if (item.fullPath === route.fullPath) { viewTags.value[index] = { ...route, ...item } - // Object.assign(item, route) } }) } @@ -56,12 +63,7 @@ export const viewTagsStore = defineStore('viewTags', () => { viewTags.value.splice(index, 1) } else { // 路由存在,更新 - viewTags.value = viewTags.value.map((item) => { - if (item.path === target.path) { - return { ...item, meta: target.meta } - } - return item - }) + updateViewTags(target) } }) }