|
|
@@ -63,9 +63,24 @@ function filterTree(data: RouteComponent[]) {
|
|
|
return newTree;
|
|
|
}
|
|
|
|
|
|
-/** 过滤没有 children 的目录,v?.children?.length > 0 确保 undefined 和空数组都被过滤 */
|
|
|
+/** 过滤没有 children 的目录,以及泄漏到一级的二级子路由 */
|
|
|
function filterChildrenTree(data: RouteComponent[]) {
|
|
|
- const newTree = cloneDeep(data).filter((v: any) => v?.children?.length > 0);
|
|
|
+ const newTree = cloneDeep(data).filter((v: any) => {
|
|
|
+ // 有非空 children → 保留(正常的父级菜单)
|
|
|
+ if (v?.children?.length > 0) return true;
|
|
|
+ // children 为空数组 → 移除
|
|
|
+ if (Array.isArray(v?.children)) return false;
|
|
|
+ // children 为 undefined:可能是合法的顶层叶子路由,也可能是泄漏的二级子路由
|
|
|
+ // 如果它的 path 是另一个顶级路由的子路径,说明是泄漏的二级路由 → 移除
|
|
|
+ const isChild = data.some(
|
|
|
+ (parent: any) =>
|
|
|
+ parent !== v &&
|
|
|
+ parent.path &&
|
|
|
+ v.path &&
|
|
|
+ v.path.startsWith(parent.path + "/")
|
|
|
+ );
|
|
|
+ return !isChild;
|
|
|
+ });
|
|
|
newTree.forEach(
|
|
|
(v: { children }) => v.children && (v.children = filterTree(v.children))
|
|
|
);
|