vue-vben-admin/src/router/index.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
import type { RouteRecordRaw } from 'vue-router';
import type { App } from 'vue';
import { createRouter, createWebHashHistory } from 'vue-router';
import { basicRoutes } from './routes';
2020-09-28 20:19:10 +08:00
// 白名单应该包含基本静态路由
const WHITE_NAME_LIST: string[] = [];
const getRouteNames = (array: any[]) =>
array.forEach((item) => {
WHITE_NAME_LIST.push(item.name);
getRouteNames(item.children || []);
});
getRouteNames(basicRoutes);
2020-09-28 20:19:10 +08:00
// app router
2022-05-28 05:49:18 +08:00
// 创建一个可以被 Vue 应用程序使用的路由实例
2021-06-07 21:30:27 +08:00
export const router = createRouter({
2022-05-28 05:49:18 +08:00
// 创建一个 hash 历史记录。
2021-03-23 23:03:29 +08:00
history: createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH),
2022-05-28 05:49:18 +08:00
// 应该添加到路由的初始路由列表。
2021-06-07 21:30:27 +08:00
routes: basicRoutes as unknown as RouteRecordRaw[],
2022-05-28 05:49:18 +08:00
// 是否应该禁止尾部斜杠。默认为假
strict: true,
2021-02-22 23:04:47 +08:00
scrollBehavior: () => ({ left: 0, top: 0 }),
2020-09-28 20:19:10 +08:00
});
2020-10-30 21:32:05 +08:00
2020-09-28 20:19:10 +08:00
// reset router
export function resetRouter() {
router.getRoutes().forEach((route) => {
const { name } = route;
if (name && !WHITE_NAME_LIST.includes(name as string)) {
2020-11-12 22:55:30 +08:00
router.hasRoute(name) && router.removeRoute(name);
2020-09-28 20:19:10 +08:00
}
});
}
// config router
// 配置路由器
2020-09-28 20:19:10 +08:00
export function setupRouter(app: App<Element>) {
app.use(router);
}