Some checks failed
CI / Test (ubuntu-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Lint (ubuntu-latest) (push) Has been cancelled
CI / Lint (windows-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Website on push / Deploy Push Playground Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Docs Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Antd Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Element Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Naive Ftp (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
CI / CI OK (push) Has been cancelled
Deploy Website on push / Rerun on failure (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Issue Close Require / close-issues (push) Has been cancelled
Close stale issues / stale (push) Has been cancelled
32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import type { Router, RouteRecordName, RouteRecordRaw } from 'vue-router';
|
||
|
||
import { traverseTreeValues } from '@vben-core/shared/utils';
|
||
|
||
/**
|
||
* @zh_CN 重置所有路由,如有指定白名单除外
|
||
*/
|
||
export function resetStaticRoutes(router: Router, routes: RouteRecordRaw[]) {
|
||
// 获取静态路由所有节点包含子节点的 name,并排除不存在 name 字段的路由
|
||
const staticRouteNames = traverseTreeValues<
|
||
RouteRecordRaw,
|
||
RouteRecordName | undefined
|
||
>(routes, (route) => {
|
||
// 这些路由需要指定 name,防止在路由重置时,不能删除没有指定 name 的路由
|
||
if (!route.name) {
|
||
console.warn(
|
||
`The route with the path ${route.path} needs to have the field name specified.`,
|
||
);
|
||
}
|
||
return route.name;
|
||
});
|
||
|
||
const { getRoutes, hasRoute, removeRoute } = router;
|
||
const allRoutes = getRoutes();
|
||
allRoutes.forEach(({ name }) => {
|
||
// 存在于路由表且非白名单才需要删除
|
||
if (name && !staticRouteNames.includes(name) && hasRoute(name)) {
|
||
removeRoute(name);
|
||
}
|
||
});
|
||
}
|