1
0

find-menu-by-path.ts 819 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import type { MenuRecordRaw } from '@vben-core/typings';
  2. function findMenuByPath(
  3. list: MenuRecordRaw[],
  4. path?: string,
  5. ): MenuRecordRaw | null {
  6. for (const menu of list) {
  7. if (menu.path === path) {
  8. return menu;
  9. }
  10. const findMenu = menu.children && findMenuByPath(menu.children, path);
  11. if (findMenu) {
  12. return findMenu;
  13. }
  14. }
  15. return null;
  16. }
  17. /**
  18. * 查找根菜单
  19. * @param menus
  20. * @param path
  21. */
  22. function findRootMenuByPath(menus: MenuRecordRaw[], path?: string) {
  23. const findMenu = findMenuByPath(menus, path);
  24. const rootMenuPath = findMenu?.parents?.[0];
  25. const rootMenu = rootMenuPath
  26. ? menus.find((item) => item.path === rootMenuPath)
  27. : undefined;
  28. return {
  29. findMenu,
  30. rootMenu,
  31. rootMenuPath,
  32. };
  33. }
  34. export { findMenuByPath, findRootMenuByPath };