Blame view

src/router/index.ts 901 Bytes
陈文彬 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, LoginRoute } from './routes';
6
import { REDIRECT_NAME } from './constant';
陈文彬 authored
7
8
9
const WHITE_NAME_LIST = [LoginRoute.name, REDIRECT_NAME];
陈文彬 authored
10
11
// app router
const router = createRouter({
Vben authored
12
  history: createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH),
vben authored
13
  routes: (basicRoutes as unknown) as RouteRecordRaw[],
14
  strict: true,
vben authored
15
  scrollBehavior: () => ({ left: 0, top: 0 }),
陈文彬 authored
16
});
vben authored
17
陈文彬 authored
18
19
20
21
// reset router
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
22
    if (name && !WHITE_NAME_LIST.includes(name as string)) {
vben authored
23
      router.hasRoute(name) && router.removeRoute(name);
陈文彬 authored
24
25
26
27
28
29
30
31
32
33
    }
  });
}

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

export default router;