|
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';
|
|
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);
|
Vben
authored
|
15
|
|
|
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[],
|
vben
authored
|
20
|
strict: true,
|
vben
authored
|
21
|
scrollBehavior: () => ({ left: 0, top: 0 }),
|
|
22
|
});
|
vben
authored
|
23
|
|
|
24
25
26
27
|
// reset router
export function resetRouter() {
router.getRoutes().forEach((route) => {
const { name } = route;
|
Vben
authored
|
28
|
if (name && !WHITE_NAME_LIST.includes(name as string)) {
|
vben
authored
|
29
|
router.hasRoute(name) && router.removeRoute(name);
|
|
30
31
32
33
34
35
36
37
|
}
});
}
// config router
export function setupRouter(app: App<Element>) {
app.use(router);
}
|