index.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import 'nprogress/css/nprogress.css'; // 进度条样式
  2. import { createRouter, createWebHashHistory } from 'vue-router';
  3. import { createRouterGuards } from './router-guards';
  4. import outsideLayout from './outsideLayout';
  5. import { whiteNameList } from './constant';
  6. import type { App } from 'vue';
  7. import type { RouteRecordRaw } from 'vue-router';
  8. export const routes: Array<RouteRecordRaw> = [
  9. {
  10. path: '/',
  11. name: 'Layout',
  12. redirect: '/welcome',
  13. component: () => import(/* webpackChunkName: "layout" */ '@/layout/index.vue'),
  14. meta: {
  15. title: '首页',
  16. },
  17. children: [],
  18. },
  19. // Layout之外的路由
  20. ...outsideLayout,
  21. ];
  22. export const router = createRouter({
  23. // process.env.BASE_URL
  24. history: createWebHashHistory(''),
  25. routes,
  26. });
  27. // reset router
  28. export function resetRouter() {
  29. router.getRoutes().forEach((route) => {
  30. const { name } = route;
  31. if (name && !whiteNameList.some((n) => n === name)) {
  32. router.hasRoute(name) && router.removeRoute(name);
  33. }
  34. });
  35. }
  36. export async function setupRouter(app: App) {
  37. // 创建路由守卫
  38. createRouterGuards(router, whiteNameList);
  39. app.use(router);
  40. // 路由准备就绪后挂载APP实例
  41. await router.isReady();
  42. }
  43. export default router;