refactor: 重构重置路由功能

This commit is contained in:
xiaoxian521
2022-08-18 18:34:53 +08:00
parent 5ac646444f
commit d6a329a63c
5 changed files with 40 additions and 16 deletions

View File

@@ -98,6 +98,19 @@ export const router: Router = createRouter({
}
});
// 重置路由
export function resetRouter() {
router.getRoutes().forEach(route => {
const { name, meta } = route;
if (name && router.hasRoute(name) && meta?.backstage) {
router.removeRoute(name);
router.options.routes = formatTwoStageRoutes(
formatFlatteningRoutes(buildHierarchyTree(ascending(routes)))
);
}
});
}
// 路由白名单
const whiteList = ["/login"];

View File

@@ -104,14 +104,14 @@ function findRouteByPath(path: string, routes: RouteRecordRaw[]) {
}
}
// 重置路由
function resetRouter(): void {
router.getRoutes().forEach(route => {
const { name } = route;
if (name) {
router.hasRoute(name) && router.removeRoute(name);
}
});
function addPathMatch() {
if (!router.hasRoute("pathMatch")) {
router.addRoute({
path: "/:pathMatch(.*)",
name: "pathMatch",
redirect: "/error/404"
});
}
}
// 初始化路由
@@ -146,10 +146,7 @@ function initRouter(name: string) {
);
usePermissionStoreHook().changeSetting(info);
}
router.addRoute({
path: "/:pathMatch(.*)",
redirect: "/error/404"
});
addPathMatch();
});
});
}
@@ -230,6 +227,8 @@ function addAsyncRoutes(arrRoutes: Array<RouteRecordRaw>) {
if (!arrRoutes || !arrRoutes.length) return;
const modulesRoutesKeys = Object.keys(modulesRoutes);
arrRoutes.forEach((v: RouteRecordRaw) => {
// 将backstage属性加入meta标识此路由为后端返回路由
v.meta.backstage = true;
if (v.redirect) {
v.component = Layout;
} else if (v.meta?.frameSrc) {
@@ -295,7 +294,6 @@ export {
ascending,
filterTree,
initRouter,
resetRouter,
hasPermissions,
getHistoryMode,
addAsyncRoutes,