index.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
  2. import { RouteRecordDetailRaw } from './routes.d';
  3. import { app_ready,app_routes } from '~/store/app';
  4. // import localRoutes from './routes/xdjx';
  5. // import localRoutes from './routes/xdjy';
  6. import localRoutes from './routes/xdhq';
  7. const router = createRouter({
  8. history: createWebHashHistory(),
  9. routes: [
  10. {
  11. path: '/',
  12. component: () => import("~/layout/loading/index.vue")
  13. },
  14. {
  15. path: '/routes',
  16. name: '-ROUTES',
  17. props: true,
  18. meta: {
  19. hidden: false,
  20. sort: -1,
  21. title: "组织机构与权限"
  22. },
  23. component: () => import("~/router/routes.vue")
  24. }
  25. ]
  26. })
  27. export default router
  28. async function handleFilterRoutes<R extends RouteRecordDetailRaw[]>(routes: R): Promise<Partial<R>> {
  29. console.log(' handleFilterRoutes ');
  30. await new Promise((resolve) => {
  31. setTimeout(() => {
  32. resolve(routes)
  33. }, 0);
  34. })
  35. return [...routes] as Partial<R>
  36. }
  37. const routes = await handleFilterRoutes(localRoutes)
  38. sessionStorage.setItem('routes', JSON.stringify(routes.concat([{
  39. path: '/routes',
  40. name: '-ROUTES',
  41. meta: {
  42. hidden: false,
  43. sort: -1,
  44. title: "组织机构与权限"
  45. },
  46. component: () => import("~/router/routes.vue")
  47. }])))
  48. routes.forEach(filteredRoute => router.addRoute(filteredRoute as RouteRecordRaw))
  49. app_ready.value = true
  50. app_routes.value = (routes as RouteRecordDetailRaw[])
  51. router.beforeEach((to, from) => {
  52. console.groupCollapsed(`%c${from.name?.toString()} => ${to.name?.toString()}`, 'color:#0ff');
  53. console.log(`%c${from.meta.title} => ${to.meta.title}`, 'color:#0cc');
  54. console.groupEnd();
  55. })