From 052eff91c4b63f4c9e08b80ae171e96d656ec607 Mon Sep 17 00:00:00 2001 From: Haceral <48341368+HaceraI@users.noreply.github.com> Date: Tue, 12 Oct 2021 09:11:25 +0800 Subject: [PATCH] perf: Improve the dynamic routing and automatically close the Tab function (#1264) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加动态路由最大打开Tab数控制 * 增加动态路由打开数控制Router参数 * feat(Tab): 新增动态路由打开数限制Demo * fix(multipleTab.ts): 将原来的打开数限制从固定的 5 修改为读取配置 Co-authored-by: Haceral <18274416193@163.com> --- src/locales/lang/en/layout.ts | 1 + src/locales/lang/en/routes/demo.ts | 1 + src/locales/lang/zh-CN/layout.ts | 1 + src/locales/lang/zh-CN/routes/demo.ts | 1 + src/router/routes/modules/demo/feat.ts | 15 ++++++++++++++ src/store/modules/multipleTab.ts | 7 ++++--- src/views/demo/feat/tabs/TabDetail.vue | 28 ++++++++++++++++++++++++++ src/views/demo/feat/tabs/index.vue | 15 +++++++++++++- 8 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 src/views/demo/feat/tabs/TabDetail.vue diff --git a/src/locales/lang/en/layout.ts b/src/locales/lang/en/layout.ts index 74b25831..5a9408a0 100644 --- a/src/locales/lang/en/layout.ts +++ b/src/locales/lang/en/layout.ts @@ -84,6 +84,7 @@ export default { breadcrumb: 'Breadcrumbs', breadcrumbIcon: 'Breadcrumbs Icon', tabs: 'Tabs', + tabDetail: 'Tab Detail', tabsQuickBtn: 'Tabs quick button', tabsRedoBtn: 'Tabs redo button', tabsFoldBtn: 'Tabs flod button', diff --git a/src/locales/lang/en/routes/demo.ts b/src/locales/lang/en/routes/demo.ts index 6941314f..b2991922 100644 --- a/src/locales/lang/en/routes/demo.ts +++ b/src/locales/lang/en/routes/demo.ts @@ -67,6 +67,7 @@ export default { feat: 'Page Function', icon: 'Icon', tabs: 'Tabs', + tabDetail: 'Tab Detail', sessionTimeout: 'Session Timeout', print: 'Print', contextMenu: 'Context Menu', diff --git a/src/locales/lang/zh-CN/layout.ts b/src/locales/lang/zh-CN/layout.ts index 5283b834..ed1f8537 100644 --- a/src/locales/lang/zh-CN/layout.ts +++ b/src/locales/lang/zh-CN/layout.ts @@ -84,6 +84,7 @@ export default { breadcrumb: '面包屑', breadcrumbIcon: '面包屑图标', tabs: '标签页', + tabDetail: '标签详情页', tabsQuickBtn: '标签页快捷按钮', tabsRedoBtn: '标签页刷新按钮', tabsFoldBtn: '标签页折叠按钮', diff --git a/src/locales/lang/zh-CN/routes/demo.ts b/src/locales/lang/zh-CN/routes/demo.ts index 4cadfd9b..0c07976b 100644 --- a/src/locales/lang/zh-CN/routes/demo.ts +++ b/src/locales/lang/zh-CN/routes/demo.ts @@ -67,6 +67,7 @@ export default { icon: '图标', sessionTimeout: '登录过期', tabs: '标签页操作', + tabDetail: '标签详情页', print: '打印', contextMenu: '右键菜单', download: '文件下载', diff --git a/src/router/routes/modules/demo/feat.ts b/src/router/routes/modules/demo/feat.ts index e3af92d1..7c7121d8 100644 --- a/src/router/routes/modules/demo/feat.ts +++ b/src/router/routes/modules/demo/feat.ts @@ -53,7 +53,22 @@ const feat: AppRouteModule = { component: () => import('/@/views/demo/feat/tabs/index.vue'), meta: { title: t('routes.demo.feat.tabs'), + hideChildrenInMenu: true, }, + children: [ + { + path: 'detail/:id', + name: 'TabDetail', + component: () => import('/@/views/demo/feat/tabs/TabDetail.vue'), + meta: { + currentActiveMenu: '/feat/tabs', + title: t('routes.demo.feat.tabDetail'), + hideMenu: true, + dynamicLevel: 3, + realPath: '/feat/tabs/detail', + }, + }, + ], }, { path: 'breadcrumb', diff --git a/src/store/modules/multipleTab.ts b/src/store/modules/multipleTab.ts index ca7863f9..023e8766 100644 --- a/src/store/modules/multipleTab.ts +++ b/src/store/modules/multipleTab.ts @@ -149,7 +149,7 @@ export const useMultipleTabStore = defineStore({ this.tabList.splice(updateIndex, 1, curTab); } else { // Add tab - // 获取动态路由层级 + // 获取动态路由打开数,超过 0 即代表需要控制打开数 const dynamicLevel = meta?.dynamicLevel ?? -1; if (dynamicLevel > 0) { // 如果动态路由层级大于 0 了,那么就要限制该路由的打开数限制了 @@ -157,8 +157,9 @@ export const useMultipleTabStore = defineStore({ // const realName: string = path.match(/(\S*)\//)![1]; const realPath = meta?.realPath ?? ''; // 获取到已经打开的动态路由数, 判断是否大于某一个值 - // 这里先固定为 每个动态路由最大能打开【5】个Tab - if (this.tabList.filter((e) => e.meta?.realPath ?? '' === realPath).length >= 5) { + if ( + this.tabList.filter((e) => e.meta?.realPath ?? '' === realPath).length >= dynamicLevel + ) { // 关闭第一个 const index = this.tabList.findIndex((item) => item.meta.realPath === realPath); index !== -1 && this.tabList.splice(index, 1); diff --git a/src/views/demo/feat/tabs/TabDetail.vue b/src/views/demo/feat/tabs/TabDetail.vue new file mode 100644 index 00000000..d768cca1 --- /dev/null +++ b/src/views/demo/feat/tabs/TabDetail.vue @@ -0,0 +1,28 @@ + + + diff --git a/src/views/demo/feat/tabs/index.vue b/src/views/demo/feat/tabs/index.vue index eccdc63e..a1bb519e 100644 --- a/src/views/demo/feat/tabs/index.vue +++ b/src/views/demo/feat/tabs/index.vue @@ -16,20 +16,28 @@ 关闭当前 刷新当前 + + + + 打开{{ index }}详情页 + +