Commit eac2fb4aaa426d9e70b161933e2366eac2a7b702

Authored by Haceral
Committed by GitHub
1 parent 7d40773b

feat: 动态路由 Tab打开数量控制,超出限制自动关闭起始Tab (#1256)

* 增加动态路由最大打开Tab数控制

* 增加动态路由打开数控制Router参数
src/store/modules/multipleTab.ts
... ... @@ -119,7 +119,7 @@ export const useMultipleTabStore = defineStore({
119 119 },
120 120  
121 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 123 // 404 The page does not need to add a tab
124 124 if (
125 125 path === PageEnum.ERROR_PAGE ||
... ... @@ -149,6 +149,21 @@ export const useMultipleTabStore = defineStore({
149 149 this.tabList.splice(updateIndex, 1, curTab);
150 150 } else {
151 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 167 this.tabList.push(route);
153 168 }
154 169 this.updateCacheTab();
... ...
types/vue-router.d.ts
... ... @@ -5,6 +5,10 @@ declare module 'vue-router' {
5 5 orderNo?: number;
6 6 // title
7 7 title: string;
  8 + // dynamic router level.
  9 + dynamicLevel?: number;
  10 + // dynamic router real route path (For performance).
  11 + realPath?: string;
8 12 // Whether to ignore permissions
9 13 ignoreAuth?: boolean;
10 14 // role info
... ...