Forráskód Böngészése

🦄 refactor(tarbar): 调整重复合并为一个tab

Zimo 2 napja
szülő
commit
49324a93bb
1 módosított fájl, 38 hozzáadás és 16 törlés
  1. 38 16
      src/store/modules/tagsView.ts

+ 38 - 16
src/store/modules/tagsView.ts

@@ -36,28 +36,50 @@ export const useTagsViewStore = defineStore('tagsView', {
       this.addCachedView()
     },
     // 新增tag
+    // addVisitedView(view: RouteLocationNormalizedLoaded) {
+    //   if (this.visitedViews.some((v) => v.fullPath === view.fullPath)) return
+    //   if (view.meta?.noTagsView) return
+    //   const visitedView = Object.assign({}, view, { title: view.meta?.title || 'no-name' })
+
+    //   if (visitedView.meta) {
+    //     const titleSuffixList: string[] = []
+    //     this.visitedViews.forEach((v) => {
+    //       if (v.path === visitedView.path && v.meta?.title === visitedView.meta?.title) {
+    //         titleSuffixList.push(v.meta?.titleSuffix || '1')
+    //       }
+    //     })
+    //     if (titleSuffixList.length) {
+    //       let titleSuffix = 1
+    //       while (titleSuffixList.includes(`${titleSuffix}`)) {
+    //         titleSuffix += 1
+    //       }
+    //       visitedView.meta.titleSuffix = titleSuffix === 1 ? undefined : `${titleSuffix}`
+    //     }
+    //   }
+
+    //   this.visitedViews.push(visitedView)
+    // },
     addVisitedView(view: RouteLocationNormalizedLoaded) {
+      // 1. 如果完全匹配(fullPath相同),说明是同一个页面状态,直接返回不处理
       if (this.visitedViews.some((v) => v.fullPath === view.fullPath)) return
+
+      // 2. 如果配置了 noTagsView,则不添加
       if (view.meta?.noTagsView) return
+
+      // 3. 处理 title
       const visitedView = Object.assign({}, view, { title: view.meta?.title || 'no-name' })
 
-      if (visitedView.meta) {
-        const titleSuffixList: string[] = []
-        this.visitedViews.forEach((v) => {
-          if (v.path === visitedView.path && v.meta?.title === visitedView.meta?.title) {
-            titleSuffixList.push(v.meta?.titleSuffix || '1')
-          }
-        })
-        if (titleSuffixList.length) {
-          let titleSuffix = 1
-          while (titleSuffixList.includes(`${titleSuffix}`)) {
-            titleSuffix += 1
-          }
-          visitedView.meta.titleSuffix = titleSuffix === 1 ? undefined : `${titleSuffix}`
-        }
-      }
+      // 4. 【核心修改】查找是否存在相同 path 的视图
+      const existIndex = this.visitedViews.findIndex((v) => v.path === visitedView.path)
 
-      this.visitedViews.push(visitedView)
+      if (existIndex > -1) {
+        // 5. 如果存在:直接替换(合并),以新的 visitedView 为主
+        // 使用 splice 确保 Vue 响应式更新,并保留该标签原本的位置
+        this.visitedViews.splice(existIndex, 1, visitedView)
+      } else {
+        // 6. 如果不存在:直接追加到末尾
+        this.visitedViews.push(visitedView)
+      }
     },
     // 新增缓存
     addCachedView() {