index.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import type { RouteRecordRaw } from 'vue-router';
  2. import type { App } from 'vue';
  3. import { createRouter, createWebHashHistory } from 'vue-router';
  4. import { basicRoutes } from './routes';
  5. // 白名单应该包含基本静态路由
  6. const WHITE_NAME_LIST: string[] = [];
  7. const getRouteNames = (array: any[]) =>
  8. array.forEach((item) => {
  9. WHITE_NAME_LIST.push(item.name);
  10. getRouteNames(item.children || []);
  11. });
  12. getRouteNames(basicRoutes);
  13. // app router
  14. export const router = createRouter({
  15. history: createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH),
  16. routes: basicRoutes as unknown as RouteRecordRaw[],
  17. strict: true,
  18. scrollBehavior: () => ({ left: 0, top: 0 }),
  19. });
  20. // reset router
  21. export function resetRouter() {
  22. router.getRoutes().forEach((route) => {
  23. const { name } = route;
  24. if (name && !WHITE_NAME_LIST.includes(name as string)) {
  25. router.hasRoute(name) && router.removeRoute(name);
  26. }
  27. });
  28. }
  29. // config router
  30. export function setupRouter(app: App<Element>) {
  31. app.use(router);
  32. }