index.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. const modules: any = import.meta.glob('./modules/**/*.ts', { eager: true });
  7. const routeModuleList: RouteRecordRaw[] = [];
  8. Object.keys(modules).forEach((key) => {
  9. const mod = modules[key].default || {};
  10. const modList = Array.isArray(mod) ? [...mod] : [mod];
  11. routeModuleList.push(...modList);
  12. });
  13. function sortRoute(a, b) {
  14. return (a.meta?.sort || 0) - (b.meta?.sort || 0);
  15. }
  16. routeModuleList.sort(sortRoute);
  17. export const RootRoute: RouteRecordRaw = {
  18. path: '/',
  19. name: 'Root',
  20. redirect: PageEnum.BASE_HOME,
  21. meta: {
  22. title: 'Root',
  23. },
  24. };
  25. export const LoginRoute: RouteRecordRaw = {
  26. path: '/login',
  27. name: 'Login',
  28. // component: () => import('@/views/login/index.vue'), //v1.x 模板
  29. // component: () => import('@/views/login/newLogin.vue'), // 2.x新模板
  30. component: () => import('@/views/login/newLogin2.vue'), // 2.x全新模板
  31. // redirect: '/',
  32. meta: {
  33. title: '登录',
  34. },
  35. };
  36. //需要验证权限
  37. export const asyncRoutes = [...routeModuleList];
  38. //普通路由 无需验证权限
  39. export const constantRouter: any[] = [LoginRoute, RootRoute, RedirectRoute];
  40. export const router = createRouter({
  41. history: createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH),
  42. routes: constantRouter,
  43. strict: true,
  44. scrollBehavior(to) {
  45. if (to.hash) {
  46. return {
  47. el: to.hash,
  48. behavior: 'smooth',
  49. };
  50. }
  51. },
  52. });
  53. export function setupRouter(app: App) {
  54. app.use(router);
  55. // 创建路由守卫
  56. createRouterGuards(router);
  57. }
  58. export default router;