Skip to content

Commit

Permalink
fix(projects): 修复路由命名为包含关系时导致导航数据出错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Particaly committed Sep 25, 2023
1 parent f6c6dbd commit 766369f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
11 changes: 4 additions & 7 deletions src/utils/router/breadcrumb.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getTopLevelMenu } from './helpers';
/**
* 获取面包屑数据
* @param activeKey - 当前页面路由的key
Expand All @@ -17,13 +18,9 @@ export function getBreadcrumbByRouteKey(activeKey: string, menus: App.GlobalMenu
*/
function getBreadcrumbMenu(activeKey: string, menus: App.GlobalMenuOption[]) {
const breadcrumbMenu: App.GlobalMenuOption[] = [];
menus.some(menu => {
const flag = activeKey.includes(menu.routeName);
if (flag) {
breadcrumbMenu.push(...getBreadcrumbMenuItem(activeKey, menu));
}
return flag;
});
const topLevelMenu = getTopLevelMenu(activeKey, menus);
const options = getBreadcrumbMenuItem(activeKey, topLevelMenu as App.GlobalMenuOption);
breadcrumbMenu.push(...options);
return breadcrumbMenu;
}

Expand Down
15 changes: 15 additions & 0 deletions src/utils/router/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ function getConstantRouteName(route: AuthRoute.Route) {
}
return names;
}

/**
* 根据路由名称查找顶级菜单
* @param routeName - 当前页面路由的key
* @param menus - 菜单数据
*/
export function getTopLevelMenu(routeName: string, menus: App.GlobalMenuOption[]): App.GlobalMenuOption | undefined {
return menus.find(item => {
if (item.routeName === routeName) return true;
if (Array.isArray(item.children)) {
return getTopLevelMenu(routeName, item.children);
}
return false;
});
}
33 changes: 22 additions & 11 deletions src/utils/router/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,29 @@ export function translateMenuLabel(menus: App.GlobalMenuOption[]): App.GlobalMen
* @param menus - 菜单数据
*/
export function getActiveKeyPathsOfMenus(activeKey: string, menus: App.GlobalMenuOption[]) {
const keys = menus.map(menu => getActiveKeyPathsOfMenu(activeKey, menu)).flat(1);
return keys;
}

function getActiveKeyPathsOfMenu(activeKey: string, menu: App.GlobalMenuOption) {
const keys: string[] = [];
if (activeKey.startsWith(menu.routeName)) {
keys.push(menu.routeName);
}
if (menu.children) {
keys.push(...menu.children.map(item => getActiveKeyPathsOfMenu(activeKey, item as App.GlobalMenuOption)).flat(1));
const keys = [] as any;
const lists = [] as any;
function traverse(list: any, parent = null) {
list.forEach((t: any) => {
lists.push(t);
if (parent) {
t.parent = parent;
}
if (t.children) {
traverse(t.children, t);
}
});
}
traverse(JSON.parse(JSON.stringify(menus)));
lists.forEach((t: App.GlobalMenuOption) => {
if (t.routeName === activeKey) {
let temp = t;
while (temp) {
keys.push(temp.routeName);
temp = temp.parent as App.GlobalMenuOption;
}
}
});
return keys;
}

Expand Down

0 comments on commit 766369f

Please sign in to comment.