Commit eac2fb4aaa426d9e70b161933e2366eac2a7b702
Committed by
GitHub
1 parent
7d40773b
feat: 动态路由 Tab打开数量控制,超出限制自动关闭起始Tab (#1256)
* 增加动态路由最大打开Tab数控制 * 增加动态路由打开数控制Router参数
Showing
2 changed files
with
20 additions
and
1 deletions
src/store/modules/multipleTab.ts
@@ -119,7 +119,7 @@ export const useMultipleTabStore = defineStore({ | @@ -119,7 +119,7 @@ export const useMultipleTabStore = defineStore({ | ||
119 | }, | 119 | }, |
120 | 120 | ||
121 | async addTab(route: RouteLocationNormalized) { | 121 | async addTab(route: RouteLocationNormalized) { |
122 | - const { path, name, fullPath, params, query } = getRawRoute(route); | 122 | + const { path, name, fullPath, params, query, meta } = getRawRoute(route); |
123 | // 404 The page does not need to add a tab | 123 | // 404 The page does not need to add a tab |
124 | if ( | 124 | if ( |
125 | path === PageEnum.ERROR_PAGE || | 125 | path === PageEnum.ERROR_PAGE || |
@@ -149,6 +149,21 @@ export const useMultipleTabStore = defineStore({ | @@ -149,6 +149,21 @@ export const useMultipleTabStore = defineStore({ | ||
149 | this.tabList.splice(updateIndex, 1, curTab); | 149 | this.tabList.splice(updateIndex, 1, curTab); |
150 | } else { | 150 | } else { |
151 | // Add tab | 151 | // Add tab |
152 | + // 获取动态路由层级 | ||
153 | + const dynamicLevel = meta?.dynamicLevel ?? -1; | ||
154 | + if (dynamicLevel > 0) { | ||
155 | + // 如果动态路由层级大于 0 了,那么就要限制该路由的打开数限制了 | ||
156 | + // 首先获取到真实的路由,使用配置方式减少计算开销. | ||
157 | + // const realName: string = path.match(/(\S*)\//)![1]; | ||
158 | + const realPath = meta?.realPath ?? ''; | ||
159 | + // 获取到已经打开的动态路由数, 判断是否大于某一个值 | ||
160 | + // 这里先固定为 每个动态路由最大能打开【5】个Tab | ||
161 | + if (this.tabList.filter((e) => e.meta?.realPath ?? '' === realPath).length >= 5) { | ||
162 | + // 关闭第一个 | ||
163 | + const index = this.tabList.findIndex((item) => item.meta.realPath === realPath); | ||
164 | + index !== -1 && this.tabList.splice(index, 1); | ||
165 | + } | ||
166 | + } | ||
152 | this.tabList.push(route); | 167 | this.tabList.push(route); |
153 | } | 168 | } |
154 | this.updateCacheTab(); | 169 | this.updateCacheTab(); |
types/vue-router.d.ts
@@ -5,6 +5,10 @@ declare module 'vue-router' { | @@ -5,6 +5,10 @@ declare module 'vue-router' { | ||
5 | orderNo?: number; | 5 | orderNo?: number; |
6 | // title | 6 | // title |
7 | title: string; | 7 | title: string; |
8 | + // dynamic router level. | ||
9 | + dynamicLevel?: number; | ||
10 | + // dynamic router real route path (For performance). | ||
11 | + realPath?: string; | ||
8 | // Whether to ignore permissions | 12 | // Whether to ignore permissions |
9 | ignoreAuth?: boolean; | 13 | ignoreAuth?: boolean; |
10 | // role info | 14 | // role info |