find-menu-by-path.ts 784 B

1234567891011121314151617181920212223242526272829303132333435
  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 = menus.find((item) => item.path === rootMenuPath);
  26. return {
  27. findMenu,
  28. rootMenu,
  29. rootMenuPath,
  30. };
  31. }
  32. export { findMenuByPath, findRootMenuByPath };