From 639a69d579cb2b988b157a8f9cd01a335539a753 Mon Sep 17 00:00:00 2001 From: Ten-K <52823142+Ten-K@users.noreply.github.com> Date: Tue, 19 Oct 2021 13:58:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(tag):=20=E5=88=A0=E9=99=A4=E9=9D=9E?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E6=BF=80=E6=B4=BBtag=E6=97=B6=E4=B8=8D?= =?UTF-8?q?=E5=88=87=E6=8D=A2tag=20(#60)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(tag): 删除非当前激活tag时不切换tag * fix(tag): 修复在激活菜单时选择关闭左侧标签后当前激活的菜单非选择菜单 * perf: 自定义去重函数更改为lodash-es的uniqBy --- src/layout/components/tag/index.vue | 26 ++++++++++++++++++++------ src/router/index.ts | 7 +++++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/layout/components/tag/index.vue b/src/layout/components/tag/index.vue index 283853067..fdeb4be34 100644 --- a/src/layout/components/tag/index.vue +++ b/src/layout/components/tag/index.vue @@ -174,9 +174,6 @@ function deleteDynamicTag(obj: any, current: any, tag?: string) { routerArrays.splice(start, end); relativeStorage.routesInStorage = routerArrays; } - router.push(obj.path); - // 删除缓存路由 - handleAliveRoute(route.matched, "delete"); }; if (tag === "other") { @@ -189,15 +186,32 @@ function deleteDynamicTag(obj: any, current: any, tag?: string) { // 从当前匹配到的路径中删除 spliceRoute(valueIndex, 1); } - - if (current === obj.path) { + let newRoute: any = routerArrays.slice(-1); + if (current === route.path) { + // 删除缓存路由 + handleAliveRoute(route.matched, "delete"); // 如果删除当前激活tag就自动切换到最后一个tag - let newRoute: any = routerArrays.slice(-1); + if (tag === "left") return; nextTick(() => { router.push({ path: newRoute[0].path }); }); + } else { + //保存跳转之前的路径 + let oldPath = route.path; + router.push(obj.path); + // 删除缓存路由 + handleAliveRoute(route.matched, "delete"); + if (!routerArrays.length) return; + let isHasOldPath = routerArrays.some(item => { + return item.path === oldPath; + }); + isHasOldPath + ? router.push(oldPath) + : router.push({ + path: newRoute[0].path + }); } } diff --git a/src/router/index.ts b/src/router/index.ts index b8d9be977..d3f7c6875 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -5,7 +5,7 @@ import { createWebHashHistory, RouteRecordNormalized } from "vue-router"; -import { split } from "lodash-es"; +import { split, uniqBy } from "lodash-es"; import { i18n } from "/@/plugins/i18n"; import { openLink } from "/@/utils/link"; import NProgress from "/@/utils/progress"; @@ -228,7 +228,10 @@ router.beforeEach((to, _from, next) => { } }); }); - storageLocal.setItem("responsive-routesInStorage", newLocalRoutes); + storageLocal.setItem( + "responsive-routesInStorage", + uniqBy(newLocalRoutes, "path") + ); }); next(); }