perf: 同步完整版分支代码

This commit is contained in:
xiaoxian521
2021-12-08 11:22:03 +08:00
parent f6057458de
commit 85a60cdc4d
14 changed files with 120 additions and 30 deletions

View File

@@ -188,6 +188,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"];
@@ -245,9 +288,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);