Blame view

src/router/index.ts 1.22 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
Jim authored
17
// 创建一个可以被 Vue 应用程序使用的路由实例
Vben authored
18
export const router = createRouter({
Jim authored
19
  // 创建一个 hash 历史记录。
Vben authored
20
  history: createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH),
Jim authored
21
  // 应该添加到路由的初始路由列表。
Vben authored
22
  routes: basicRoutes as unknown as RouteRecordRaw[],
Jim authored
23
  // 是否应该禁止尾部斜杠。默认为假
24
  strict: true,
vben authored
25
  scrollBehavior: () => ({ left: 0, top: 0 }),
陈文彬 authored
26
});
vben authored
27
陈文彬 authored
28
29
30
31
// reset router
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
32
    if (name && !WHITE_NAME_LIST.includes(name as string)) {
vben authored
33
      router.hasRoute(name) && router.removeRoute(name);
陈文彬 authored
34
35
36
37
38
    }
  });
}

// config router
39
// 配置路由器
陈文彬 authored
40
41
42
export function setupRouter(app: App<Element>) {
  app.use(router);
}