index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * @Author: renxin renxin0828@163.com
  3. * @Date: 2023-03-21 17:30:01
  4. * @LastEditors: renxin renxin0828@163.com
  5. * @LastEditTime: 2023-03-22 17:36:30
  6. * @FilePath: \thingskit-front\src\router\index.ts
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. import type { RouteRecordRaw } from 'vue-router';
  10. import type { App } from 'vue';
  11. import { createRouter, createWebHistory } from 'vue-router';
  12. import { basicRoutes } from './routes';
  13. // 白名单应该包含基本静态路由
  14. const WHITE_NAME_LIST: string[] = [];
  15. const getRouteNames = (array: any[]) =>
  16. array.forEach((item) => {
  17. WHITE_NAME_LIST.push(item.name);
  18. getRouteNames(item.children || []);
  19. });
  20. getRouteNames(basicRoutes);
  21. // app router
  22. export const router = createRouter({
  23. history: createWebHistory(import.meta.env.VITE_GLOB_PUBLIC_PATH),
  24. routes: basicRoutes as unknown as RouteRecordRaw[],
  25. strict: true,
  26. scrollBehavior: () => ({ left: 0, top: 0 }),
  27. });
  28. // reset router
  29. export function resetRouter() {
  30. router.getRoutes().forEach((route) => {
  31. const { name } = route;
  32. if (name && !WHITE_NAME_LIST.includes(name as string)) {
  33. // 检查路由是否存在 &&通过名称删除现有路由。
  34. router.hasRoute(name) && router.removeRoute(name);
  35. }
  36. });
  37. }
  38. // config router
  39. export function setupRouter(app: App<Element>) {
  40. app.use(router);
  41. }