Blame view

src/router/index.ts 1 KB
陈文彬 authored
1
2
3
4
import type { RouteRecordRaw } from 'vue-router';
import type { App } from 'vue';

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

// config router
export function setupRouter(app: App<Element>) {
  app.use(router);
}