routeHelper.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import type { AppRouteModule, AppRouteRecordRaw } from '/@/router/types';
  2. import type { RouteRecordRaw } from 'vue-router';
  3. import { appStore } from '/@/store/modules/app';
  4. import { tabStore } from '/@/store/modules/tab';
  5. import { createRouter, createWebHashHistory } from 'vue-router';
  6. import { toRaw } from 'vue';
  7. import { PAGE_LAYOUT_COMPONENT } from '/@/router/constant';
  8. export function genRouteModule(moduleList: AppRouteModule[]) {
  9. const ret: AppRouteRecordRaw[] = [];
  10. for (const routeMod of moduleList) {
  11. const routes = routeMod.routes as any;
  12. const layout = routeMod.layout;
  13. const router = createRouter({ routes, history: createWebHashHistory() });
  14. const flatList = toRaw(router.getRoutes()).filter((item) => item.children.length === 0);
  15. try {
  16. (router as any) = null;
  17. } catch (error) {}
  18. flatList.forEach((item) => {
  19. item.path = `${layout.path}${item.path}`;
  20. });
  21. layout.children = (flatList as unknown) as AppRouteRecordRaw[];
  22. ret.push(layout);
  23. }
  24. return ret as RouteRecordRaw[];
  25. }
  26. function asyncImportRoute(routes: AppRouteRecordRaw[]) {
  27. routes.forEach((item) => {
  28. const { component, children } = item;
  29. if (component) {
  30. item.component = () => import(`/@/views/${component}`);
  31. }
  32. children && asyncImportRoute(children);
  33. });
  34. }
  35. export function transformObjToRoute(routeList: AppRouteModule[]) {
  36. routeList.forEach((route) => {
  37. asyncImportRoute(route.routes);
  38. if (route.layout) {
  39. route.layout.component =
  40. route.layout.component === 'PAGE_LAYOUT' ? PAGE_LAYOUT_COMPONENT : '';
  41. }
  42. });
  43. return routeList;
  44. }
  45. export function getIsOpenTab(toPath: string) {
  46. const { openKeepAlive, multiTabsSetting: { show } = {} } = appStore.getProjectConfig;
  47. if (show && openKeepAlive) {
  48. const tabList = tabStore.getTabsState;
  49. return tabList.some((tab) => tab.path === toPath);
  50. }
  51. return false;
  52. }