From f9ff87c70898aae74ecc67ffedd4a5d264c0aaae Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Mon, 19 Apr 2021 19:22:24 +0800 Subject: [PATCH] feat: add refresh route --- src/layout/components/tag/tagsHook.ts | 77 +++--- src/layout/index.vue | 16 +- src/router/index.ts | 379 +++++++++++++++----------- src/utils/algorithm/index.ts | 14 +- src/views/redirect.vue | 26 ++ 5 files changed, 304 insertions(+), 208 deletions(-) create mode 100644 src/views/redirect.vue diff --git a/src/layout/components/tag/tagsHook.ts b/src/layout/components/tag/tagsHook.ts index 254e19aaf..83158f0f2 100644 --- a/src/layout/components/tag/tagsHook.ts +++ b/src/layout/components/tag/tagsHook.ts @@ -1,85 +1,88 @@ -import { reactive, toRefs, nextTick } from "vue" -import { storageLocal } from "/@/utils/storage" -import { useRouter } from "vue-router" +import { reactive, toRefs, nextTick } from "vue"; +import { storageLocal } from "/@/utils/storage"; +import { useRouter } from "vue-router"; interface InterDynamic { - dRoutes: object[], - [propName: string]: any + dRoutes: object[]; + [propName: string]: any; } // 默认显示首页tag let dynamic: InterDynamic = reactive({ dRoutes: [ { - path: "/welcome", meta: { + path: "/welcome", + meta: { title: "home", - icon: 'el-icon-s-home', + icon: "el-icon-s-home", showLink: true, savedPosition: false, - } - }] -}) + }, + }, + ], +}); export function useDynamicRoutesHook() { - const router = useRouter() + const router = useRouter(); /** * @param value string 当前menu对应的路由path * @param parentPath string 当前路由中父级路由 */ const dynamicRouteTags = (value: string, parentPath: string): void => { const hasValue = dynamic.dRoutes.some((item: any) => { - return item.path === value - }) + return item.path === value; + }); function concatPath(arr: object[], value: string, parentPath: string) { if (!hasValue) { arr.forEach((arrItem: any) => { - let pathConcat = parentPath + '/' + arrItem.path + let pathConcat = parentPath + "/" + arrItem.path; if (arrItem.path === value || pathConcat === value) { - dynamic.dRoutes.push({ path: value, meta: arrItem.meta }) + dynamic.dRoutes.push({ path: value, meta: arrItem.meta }); // console.log(dynamic.dRoutes) } else { if (arrItem.children && arrItem.children.length > 0) { - concatPath(arrItem.children, value, parentPath) + concatPath(arrItem.children, value, parentPath); } } - }) + }); } } - concatPath(router.options.routes, value, parentPath) + concatPath(router.options.routes, value, parentPath); - if (storageLocal.getItem("routesInStorage") && storageLocal.getItem("routesInStorage").length > 2) { - let lens = storageLocal.getItem("routesInStorage").length - let itemss = storageLocal.getItem("routesInStorage")[lens - 1] - dynamic.dRoutes.push({ path: itemss.path, meta: itemss.meta }) - } + // if (storageLocal.getItem("routesInStorage") && storageLocal.getItem("routesInStorage").length > 2) { + // let lens = storageLocal.getItem("routesInStorage").length + // let itemss = storageLocal.getItem("routesInStorage")[lens - 1] + // dynamic.dRoutes.push({ path: itemss.path, meta: itemss.meta }) + // } - storageLocal.setItem("routesInStorage", dynamic.dRoutes) - } + // storageLocal.setItem("routesInStorage", dynamic.dRoutes) + }; /** * @param value any 当前删除tag路由 * @param current objct 当前激活路由对象 */ const deleteDynamicTag = async (obj: any, current: object): Promise => { let valueIndex: number = dynamic.dRoutes.findIndex((item: any) => { - return item.path === obj.path - }) + return item.path === obj.path; + }); // 从当前匹配到的路径中删除 - await dynamic.dRoutes.splice(valueIndex, 1) - storageLocal.setItem("routesInStorage", dynamic.dRoutes) - if (current === obj.path) { // 如果删除当前激活tag就自动切换到最后一个tag - let newRoute: any = dynamic.dRoutes.slice(-1) + await dynamic.dRoutes.splice(valueIndex, 1); + // storageLocal.setItem("routesInStorage", dynamic.dRoutes) + if (current === obj.path) { + // 如果删除当前激活tag就自动切换到最后一个tag + let newRoute: any = dynamic.dRoutes.slice(-1); nextTick(() => { router.push({ - path: newRoute[0].path - }) - }) + path: newRoute[0].path, + }); + }); } - } + }; return { ...toRefs(dynamic), dynamicRouteTags, - deleteDynamicTag - } + deleteDynamicTag, + }; } diff --git a/src/layout/index.vue b/src/layout/index.vue index 7f852a3dd..9f1b67af1 100644 --- a/src/layout/index.vue +++ b/src/layout/index.vue @@ -16,7 +16,7 @@