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,10 +95,13 @@ const isLastView = () => {
};
const filterAffixTags = (routes: RouteRecordRaw[], basePath = '') => {
let tags: RouteLocationNormalized[] = [];
const seen = new Set<string>();
routes.forEach((route) => {
if (route.meta && route.meta.affix) {
const tagPath = getNormalPath(basePath + '/' + route.path);
if (!seen.has(tagPath)) {
seen.add(tagPath);
tags.push({
hash: '',
matched: [],
@@ -111,6 +114,7 @@ const filterAffixTags = (routes: RouteRecordRaw[], basePath = '') => {
meta: { ...route.meta }
});
}
}
if (route.children) {
const tempTags = filterAffixTags(route.children, route.path);
if (tempTags.length >= 1) {
@@ -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);
}
};

View File

@@ -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'