feat 优化标签

This commit is contained in:
Ironsp
2026-06-23 16:10:05 +08:00
parent 80a360fa0e
commit e4d014cf8c
2 changed files with 25 additions and 13 deletions

View File

@@ -95,21 +95,25 @@ const isLastView = () => {
}; };
const filterAffixTags = (routes: RouteRecordRaw[], basePath = '') => { const filterAffixTags = (routes: RouteRecordRaw[], basePath = '') => {
let tags: RouteLocationNormalized[] = []; let tags: RouteLocationNormalized[] = [];
const seen = new Set<string>();
routes.forEach((route) => { routes.forEach((route) => {
if (route.meta && route.meta.affix) { if (route.meta && route.meta.affix) {
const tagPath = getNormalPath(basePath + '/' + route.path); const tagPath = getNormalPath(basePath + '/' + route.path);
tags.push({ if (!seen.has(tagPath)) {
hash: '', seen.add(tagPath);
matched: [], tags.push({
params: undefined, hash: '',
query: undefined, matched: [],
redirectedFrom: undefined, params: undefined,
fullPath: tagPath, query: undefined,
path: tagPath, redirectedFrom: undefined,
name: route.name as string, fullPath: tagPath,
meta: { ...route.meta } path: tagPath,
}); name: route.name as string,
meta: { ...route.meta }
});
}
} }
if (route.children) { if (route.children) {
const tempTags = filterAffixTags(route.children, route.path); const tempTags = filterAffixTags(route.children, route.path);
@@ -124,7 +128,6 @@ const initTags = () => {
const res = filterAffixTags(routes.value); const res = filterAffixTags(routes.value);
affixTags.value = res; affixTags.value = res;
for (const tag of res) { for (const tag of res) {
// Must have tag name
if (tag.name) { if (tag.name) {
useTagsViewStore().addVisitedView(tag); useTagsViewStore().addVisitedView(tag);
} }
@@ -136,6 +139,10 @@ const addTags = () => {
route.meta.title = route.query.title as string; route.meta.title = route.query.title as string;
} }
if (name) { 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); useTagsViewStore().addView(route as any);
} }
}; };

View File

@@ -37,7 +37,12 @@ export const useTagsViewStore = defineStore('tagsView', () => {
}); });
}; };
const addVisitedView = (view: RouteLocationNormalized): void => { 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( visitedViews.value.push(
Object.assign({}, view, { Object.assign({}, view, {
title: view.meta?.title || 'no-name' title: view.meta?.title || 'no-name'