From d49b23e8f961d0956d10adf3cc6ab44424341ca2 Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Thu, 4 May 2023 17:43:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D`params`=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E4=BC=A0=E5=8F=82=E6=A8=A1=E5=BC=8F=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E9=9D=A2=E5=8C=85=E5=B1=91=E6=97=A0=E6=B3=95=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E7=88=B6=E7=BA=A7=E8=B7=AF=E5=BE=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/components/sidebar/breadCrumb.vue | 7 ++++++- src/router/utils.ts | 16 ++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/layout/components/sidebar/breadCrumb.vue b/src/layout/components/sidebar/breadCrumb.vue index 242530b17..92f186f74 100644 --- a/src/layout/components/sidebar/breadCrumb.vue +++ b/src/layout/components/sidebar/breadCrumb.vue @@ -38,9 +38,14 @@ const getBreadcrumb = (): void => { currentRoute = findRouteByPath(router.currentRoute.value.path, multiTags); } // 当前路由的父级路径组成的数组 - const parentRoutes = getParentPaths(router.currentRoute.value.path, routes); + const parentRoutes = getParentPaths( + router.currentRoute.value.name as string, + routes, + "name" + ); // 存放组成面包屑的数组 let matched = []; + // 获取每个父级路径对应的路由信息 parentRoutes.forEach(path => { if (path !== "/") matched.push(findRouteByPath(path, routes)); diff --git a/src/router/utils.ts b/src/router/utils.ts index 32c0e6e90..91c30c7c2 100644 --- a/src/router/utils.ts +++ b/src/router/utils.ts @@ -92,20 +92,20 @@ function filterNoPermissionTree(data: RouteComponent[]) { return filterChildrenTree(newTree); } -/** 通过path获取父级路径 */ -function getParentPaths(path: string, routes: RouteRecordRaw[]) { +/** 通过指定 `key` 获取父级路径集合,默认 `key` 为 `path` */ +function getParentPaths(value: string, routes: RouteRecordRaw[], key = "path") { // 深度遍历查找 - function dfs(routes: RouteRecordRaw[], path: string, parents: string[]) { + function dfs(routes: RouteRecordRaw[], value: string, parents: string[]) { for (let i = 0; i < routes.length; i++) { const item = routes[i]; - // 找到path则返回父级path - if (item.path === path) return parents; + // 返回父级path + if (item[key] === value) return parents; // children不存在或为空则不递归 if (!item.children || !item.children.length) continue; // 往下查找时将当前path入栈 parents.push(item.path); - if (dfs(item.children, path, parents).length) return parents; + if (dfs(item.children, value, parents).length) return parents; // 深度遍历查找未找到时当前path 出栈 parents.pop(); } @@ -113,10 +113,10 @@ function getParentPaths(path: string, routes: RouteRecordRaw[]) { return []; } - return dfs(routes, path, []); + return dfs(routes, value, []); } -/** 查找对应path的路由信息 */ +/** 查找对应 `path` 的路由信息 */ function findRouteByPath(path: string, routes: RouteRecordRaw[]) { let res = routes.find((item: { path: string }) => item.path == path); if (res) {