diff --git a/src/layout/components/TagsView/index.vue b/src/layout/components/TagsView/index.vue index 2d74581..dc7661b 100644 --- a/src/layout/components/TagsView/index.vue +++ b/src/layout/components/TagsView/index.vue @@ -95,21 +95,25 @@ const isLastView = () => { }; const filterAffixTags = (routes: RouteRecordRaw[], basePath = '') => { let tags: RouteLocationNormalized[] = []; + const seen = new Set(); routes.forEach((route) => { if (route.meta && route.meta.affix) { const tagPath = getNormalPath(basePath + '/' + route.path); - tags.push({ - hash: '', - matched: [], - params: undefined, - query: undefined, - redirectedFrom: undefined, - fullPath: tagPath, - path: tagPath, - name: route.name as string, - meta: { ...route.meta } - }); + if (!seen.has(tagPath)) { + seen.add(tagPath); + tags.push({ + hash: '', + matched: [], + params: undefined, + query: undefined, + redirectedFrom: undefined, + fullPath: tagPath, + path: tagPath, + name: route.name as string, + meta: { ...route.meta } + }); + } } if (route.children) { const tempTags = filterAffixTags(route.children, route.path); @@ -124,7 +128,6 @@ const initTags = () => { const res = filterAffixTags(routes.value); affixTags.value = res; for (const tag of res) { - // Must have tag name if (tag.name) { useTagsViewStore().addVisitedView(tag); } @@ -136,6 +139,10 @@ const addTags = () => { route.meta.title = route.query.title as string; } if (name) { + // 如果 visitedViews 中已有同 title 的固定标签(affix),不再添加普通标签 + if (visitedViews.value.some((v) => v.meta?.affix && v.title === route.meta?.title)) return; + // 常规去重:已有同 path 或 fullPath 的标签则跳过 + if (visitedViews.value.some((v) => v.path === route.path || v.fullPath === route.fullPath)) return; useTagsViewStore().addView(route as any); } }; diff --git a/src/store/modules/tagsView.ts b/src/store/modules/tagsView.ts index dc8850c..21f1f54 100644 --- a/src/store/modules/tagsView.ts +++ b/src/store/modules/tagsView.ts @@ -37,7 +37,12 @@ export const useTagsViewStore = defineStore('tagsView', () => { }); }; const addVisitedView = (view: RouteLocationNormalized): void => { - if (visitedViews.value.some((v: RouteLocationNormalized) => v.path === view.path)) return; + // 同时通过 path、fullPath、name 去重 + if (visitedViews.value.some((v: RouteLocationNormalized) => + v.path === view.path || + v.fullPath === view.fullPath || + (v.name && view.name && v.name === view.name) + )) return; visitedViews.value.push( Object.assign({}, view, { title: view.meta?.title || 'no-name'