index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 { exceptionRouters, HOME_PAGE, platformRoutes, 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 constantRoutes = [RootRoute, HOME_PAGE, RedirectRoute, exceptionRouters];
  20. //需要验证权限
  21. export const asyncRoutes = [...routeModuleList];
  22. const routeList = routerGenerator(constantRoutes);
  23. asyncImportRoute(routeList);
  24. //普通路由 无需验证权限
  25. export const constantRouter: any[] = routeList;
  26. export const router = createRouter({
  27. history: createWebHashHistory(),
  28. routes: constantRouter,
  29. strict: true,
  30. scrollBehavior(to) {
  31. if (to.hash) {
  32. return {
  33. el: to.hash,
  34. behavior: 'smooth',
  35. };
  36. }
  37. },
  38. });
  39. export function setupRouter(app: App) {
  40. app.use(router);
  41. // 创建路由守卫
  42. createRouterGuards(router);
  43. }
  44. export default router;