Skip to content

Commit 8255f90

Browse files
authoredApr 21, 2025··
fix: 修复resetRouter未清空全部路由数据 (#1208)
1 parent 59319aa commit 8255f90

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed
 

‎src/router/index.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import { buildHierarchyTree } from "@/utils/tree";
77
import remainingRouter from "./modules/remaining";
88
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
99
import { usePermissionStoreHook } from "@/store/modules/permission";
10-
import { isUrl, openLink, storageLocal, isAllEmpty } from "@pureadmin/utils";
10+
import {
11+
isUrl,
12+
openLink,
13+
cloneDeep,
14+
isAllEmpty,
15+
storageLocal
16+
} from "@pureadmin/utils";
1117
import {
1218
ascending,
1319
getTopMenu,
@@ -21,9 +27,9 @@ import {
2127
} from "./utils";
2228
import {
2329
type Router,
24-
createRouter,
2530
type RouteRecordRaw,
26-
type RouteComponent
31+
type RouteComponent,
32+
createRouter
2733
} from "vue-router";
2834
import {
2935
type DataInfo,
@@ -55,6 +61,9 @@ export const constantRoutes: Array<RouteRecordRaw> = formatTwoStageRoutes(
5561
formatFlatteningRoutes(buildHierarchyTree(ascending(routes.flat(Infinity))))
5662
);
5763

64+
/** 初始的静态路由,用于退出登陆时重置路由 */
65+
const initConstantRoutes: Array<RouteRecordRaw> = cloneDeep(constantRoutes);
66+
5867
/** 用于渲染菜单,保持原始层级 */
5968
export const constantMenus: Array<RouteComponent> = ascending(
6069
routes.flat(Infinity)
@@ -87,17 +96,13 @@ export const router: Router = createRouter({
8796

8897
/** 重置路由 */
8998
export function resetRouter() {
90-
router.getRoutes().forEach(route => {
91-
const { name, meta } = route;
92-
if (name && router.hasRoute(name) && meta?.backstage) {
93-
router.removeRoute(name);
94-
router.options.routes = formatTwoStageRoutes(
95-
formatFlatteningRoutes(
96-
buildHierarchyTree(ascending(routes.flat(Infinity)))
97-
)
98-
);
99-
}
100-
});
99+
router.clearRoutes();
100+
for (const route of initConstantRoutes.concat(...(remainingRouter as any))) {
101+
router.addRoute(route);
102+
}
103+
router.options.routes = formatTwoStageRoutes(
104+
formatFlatteningRoutes(buildHierarchyTree(ascending(routes.flat(Infinity))))
105+
);
101106
usePermissionStoreHook().clearAllCachePage();
102107
}
103108

‎src/router/utils.ts

+2
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ function handleAsyncRoutes(routeList) {
172172
const flattenRouters: any = router
173173
.getRoutes()
174174
.find(n => n.path === "/");
175+
// 保持router.options.routes[0].children与path为"/"的children一致,防止数据不一致导致异常
176+
flattenRouters.children = router.options.routes[0].children;
175177
router.addRoute(flattenRouters);
176178
}
177179
}

1 commit comments

Comments
 (1)

xiaoxian521 commented on Apr 21, 2025

@xiaoxian521
Member

Image

Please sign in to comment.