index.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
  2. import { app_ready, app_routes } from '~/store/app';
  3. import { RouteRecordDetailRaw } from './routes.d';
  4. import localRoutes, { title } from './routes/xdjx';
  5. // import localRoutes, { title } from './routes/xdjy';
  6. // import localRoutes, { title } from './routes/xdhq';
  7. document.title = title
  8. // !auto switch routes way 1:
  9. // !build error
  10. // const localRoutes = (await import(`./routes/${import.meta.env.MODE}.ts`))['default'] as RouteRecordDetailRaw[]
  11. // const currentRouteModule = import.meta.glob(`./routes/*.ts`, { eager: true })[`./routes/${import.meta.env.MODE}.ts`] as { title: string, default: RouteRecordDetailRaw[] }
  12. // document.title = currentRouteModule['title']
  13. // const localRoutes = currentRouteModule['default']
  14. console.log('localRoutes :>> ', localRoutes);
  15. const router = createRouter({
  16. history: createWebHashHistory(),
  17. routes: [
  18. {
  19. path: '/',
  20. component: () => import("~/layout/loading/index.vue")
  21. },
  22. {
  23. path: '/routes',
  24. name: '-ROUTES',
  25. props: true,
  26. meta: {
  27. hidden: false,
  28. sort: -1,
  29. title: "组织机构与权限"
  30. },
  31. component: () => import("~/router/routes.vue")
  32. },
  33. {
  34. path: '/xxzx',
  35. name: 'xxzx',
  36. props: true,
  37. meta: {
  38. hidden: true,
  39. sort: -1,
  40. title: "消息中心"
  41. },
  42. component: () => import("~/pages/[...]/xxzx/index.vue")
  43. }
  44. ]
  45. })
  46. export default router
  47. async function handleFilterRoutes<R extends RouteRecordDetailRaw[]>(routes: R): Promise<Partial<R>> {
  48. console.log(' handleFilterRoutes ');
  49. await new Promise((resolve) => {
  50. setTimeout(() => {
  51. resolve(routes)
  52. }, 0);
  53. })
  54. return [...routes] as Partial<R>
  55. }
  56. const routes = await handleFilterRoutes(localRoutes)
  57. sessionStorage.setItem('routes', JSON.stringify(routes.concat([{
  58. path: '/routes',
  59. name: '-ROUTES',
  60. meta: {
  61. hidden: false,
  62. sort: -1,
  63. title: "组织机构与权限"
  64. },
  65. component: () => import("~/router/routes.vue")
  66. }])))
  67. routes.forEach(filteredRoute => router.addRoute(filteredRoute as RouteRecordRaw))
  68. app_ready.value = true
  69. app_routes.value = (routes as RouteRecordDetailRaw[])
  70. router.beforeEach((to, from) => {
  71. console.groupCollapsed(`%c${from.name?.toString()} => ${to.name?.toString()}`, 'color:#0ff');
  72. console.log(`%c${from.meta.title} => ${to.meta.title}`, 'color:#0cc');
  73. console.groupEnd();
  74. })