CET-vue-3.0/src/router/index.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-10-20 14:32:09 +08:00
import type { RouteRecordRaw } from 'vue-router';
import type { App } from 'vue';
2022-03-10 09:47:29 +08:00
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router';
2021-10-20 14:32:09 +08:00
import { basicRoutes } from './routes';
// 白名单应该包含基本静态路由
const WHITE_NAME_LIST: string[] = [];
const getRouteNames = (array: any[]) =>
2022-06-10 10:44:44 +08:00
array.forEach((item) => {
WHITE_NAME_LIST.push(item.name);
getRouteNames(item.children || []);
});
2021-10-20 14:32:09 +08:00
getRouteNames(basicRoutes);
// app router
export const router = createRouter({
2022-03-10 09:47:29 +08:00
history: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
2021-10-20 14:32:09 +08:00
routes: basicRoutes as unknown as RouteRecordRaw[],
strict: true,
scrollBehavior: () => ({ left: 0, top: 0 }),
});
2023-10-18 14:55:25 +08:00
// TODO 【QQYUN-4517】【表单设计器】记录分享路由守卫测试
router.beforeEach(async (to, from, next) => {
//console.group('【QQYUN-4517】beforeEach');
//console.warn('from', from);
//console.warn('to', to);
//console.groupEnd();
next();
});
2021-10-20 14:32:09 +08:00
// reset router
export function resetRouter() {
router.getRoutes().forEach((route) => {
const { name } = route;
if (name && !WHITE_NAME_LIST.includes(name as string)) {
router.hasRoute(name) && router.removeRoute(name);
}
});
}
// config router
export function setupRouter(app: App<Element>) {
app.use(router);
}