index.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { App } from 'vue';
  2. import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
  3. import { RedirectRoute } from '@/router/base';
  4. import { PageEnum } from '@/enums/pageEnum';
  5. import { createRouterGuards } from './router-guards';
  6. import { disasterPreventionRoute, exceptionRouters, fullRoutes, HOME_PAGE, RootRoute } from './full-routes';
  7. import { asyncImportRoute, routerGenerator } from './generator-routers';
  8. const modules: any = import.meta.glob('./modules/**/*.ts', { eager: true });
  9. const routeModuleList: RouteRecordRaw[] = [];
  10. Object.keys(modules).forEach((key) => {
  11. const mod = modules[key].default || {};
  12. const modList = Array.isArray(mod) ? [...mod] : [mod];
  13. routeModuleList.push(...modList);
  14. });
  15. function sortRoute(a, b) {
  16. return (a.meta?.sort || 0) - (b.meta?.sort || 0);
  17. }
  18. routeModuleList.sort(sortRoute);
  19. const { children, ...disasterPreventionTopRoute } = disasterPreventionRoute;
  20. const constantRoutes = [
  21. RootRoute,
  22. HOME_PAGE,
  23. disasterPreventionTopRoute,
  24. RedirectRoute,
  25. exceptionRouters,
  26. ...fullRoutes,
  27. ];
  28. //需要验证权限
  29. export const asyncRoutes = [...routeModuleList];
  30. const routeList = routerGenerator(constantRoutes);
  31. asyncImportRoute(routeList);
  32. //普通路由 无需验证权限
  33. export const constantRouter: any[] = routeList;
  34. export const router = createRouter({
  35. history: createWebHashHistory(),
  36. routes: constantRouter,
  37. strict: true,
  38. scrollBehavior(to) {
  39. if (to.hash) {
  40. return {
  41. el: to.hash,
  42. behavior: 'smooth',
  43. };
  44. }
  45. },
  46. });
  47. export function setupRouter(app: App) {
  48. app.use(router);
  49. // 创建路由守卫
  50. createRouterGuards(router);
  51. }
  52. export default router;