index.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import type { RouteRecordRaw } from 'vue-router';
  2. import type { App } from 'vue';
  3. import { basicRoutes } from './routes';
  4. import { createRouter as createVueRouter, destroyRouter, router } from './router';
  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. /**
  14. * 创建路由实例
  15. */
  16. export function createRouter() {
  17. const router = createVueRouter({
  18. routes: basicRoutes as unknown as RouteRecordRaw[],
  19. strict: true,
  20. scrollBehavior: () => ({ left: 0, top: 0 }),
  21. });
  22. // TODO 【QQYUN-4517】【表单设计器】记录分享路由守卫测试
  23. // @ts-ignore
  24. router.beforeEach(async (to, from, next) => {
  25. //console.group('【QQYUN-4517】beforeEach');
  26. //console.warn('from', from);
  27. //console.warn('to', to);
  28. //console.groupEnd();
  29. next();
  30. });
  31. }
  32. // reset router
  33. export function resetRouter() {
  34. router.getRoutes().forEach((route) => {
  35. const { name } = route;
  36. if (name && !WHITE_NAME_LIST.includes(name as string)) {
  37. router.hasRoute(name) && router.removeRoute(name);
  38. }
  39. });
  40. }
  41. // config router
  42. export function setupRouter(app: App<Element>) {
  43. app.use(router);
  44. }
  45. export { router, destroyRouter };