mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-11-09 13:53:38 +08:00
perf(router): refresh (#142)
* feat: set add multiTagsCache * perf(router): refresh * fix(multiTags): clear storage when close other tag Co-authored-by: lrl <742798240@qq.com>
This commit is contained in:
@@ -199,6 +199,49 @@ export function resetRouter() {
|
||||
});
|
||||
}
|
||||
|
||||
function findRouteByPath(path, routes) {
|
||||
let res = routes.find(item => item.path == path);
|
||||
if (res) {
|
||||
return res;
|
||||
} else {
|
||||
for (let i = 0; i < routes.length; i++) {
|
||||
if (
|
||||
routes[i].children instanceof Array &&
|
||||
routes[i].children.length > 0
|
||||
) {
|
||||
res = findRouteByPath(path, routes[i].children);
|
||||
if (res) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function getParentPaths(path, routes) {
|
||||
// 深度遍历查找
|
||||
function dfs(routes, path, parents) {
|
||||
for (let i = 0; i < routes.length; i++) {
|
||||
const item = routes[i];
|
||||
// 找到path则返回父级path
|
||||
if (item.path === path) return parents;
|
||||
// children不存在或为空则不递归
|
||||
if (!item.children || !item.children.length) continue;
|
||||
// 往下查找时将当前path入栈
|
||||
parents.push(item.path);
|
||||
|
||||
if (dfs(item.children, path, parents).length) return parents;
|
||||
// 深度遍历查找未找到时当前path 出栈
|
||||
parents.pop();
|
||||
}
|
||||
// 未找到时返回空数组
|
||||
return [];
|
||||
}
|
||||
|
||||
return dfs(routes, path, []);
|
||||
}
|
||||
|
||||
// 路由白名单
|
||||
const whiteList = ["/login"];
|
||||
|
||||
@@ -256,9 +299,17 @@ router.beforeEach((to, _from, next) => {
|
||||
handTag(path, parentPath, name, meta);
|
||||
return router.push(path);
|
||||
} else {
|
||||
const { path, name, meta } = to;
|
||||
handTag(path, parentPath, name, meta);
|
||||
return router.push(to.path);
|
||||
const { path } = to;
|
||||
const routes = router.options.routes;
|
||||
const route = findRouteByPath(path, routes);
|
||||
const routePartent = getParentPaths(path, routes);
|
||||
handTag(
|
||||
route.path,
|
||||
routePartent[routePartent.length - 1],
|
||||
route.name,
|
||||
route.meta
|
||||
);
|
||||
return router.push(path);
|
||||
}
|
||||
}
|
||||
router.push(to.path);
|
||||
|
||||
Reference in New Issue
Block a user