路由修正
This commit is contained in:
@@ -1,33 +1,40 @@
|
||||
import { RouteRecordRaw, RouteLocationNormalized } from 'vue-router';
|
||||
|
||||
export function getParentChildrenRoutes(route: RouteLocationNormalized): RouteRecordRaw[] {
|
||||
// 判断是否有父节点(至少需要2个匹配的路由)
|
||||
if (route.matched.length < 2) {
|
||||
// 没有任何匹配路由,直接返回空
|
||||
if (!route.matched || route.matched.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// 获取倒数第二个匹配的路由(父路由)
|
||||
const parentRoute = route.matched[route.matched.length - 2];
|
||||
|
||||
// 检查父路由是否有子路由
|
||||
if (!parentRoute?.children || parentRoute.children.length === 0) {
|
||||
return [];
|
||||
|
||||
// 从当前路由向上查找,找到第一个“有可见子路由”的父级
|
||||
for (let i = route.matched.length - 2; i >= 0; i--) {
|
||||
const parentRoute = route.matched[i];
|
||||
const children = parentRoute?.children || [];
|
||||
|
||||
if (!children || children.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const visibleChildren = children.filter((child: RouteRecordRaw) => {
|
||||
// 必须有 title
|
||||
if (!child.meta?.title) {
|
||||
return false;
|
||||
}
|
||||
// 排除 path 为空字符串的子路由(通常是默认子路由)
|
||||
if (child.path === '' || child.path === undefined) {
|
||||
return false;
|
||||
}
|
||||
// 排除 name 以 _page 结尾的子路由(父路由的默认子路由)
|
||||
if (typeof child.name === 'string' && child.name.endsWith('_page')) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
if (visibleChildren.length > 0) {
|
||||
return visibleChildren;
|
||||
}
|
||||
}
|
||||
|
||||
// 返回有 title 的子路由,但排除父路由本身(path 为空字符串或 name 以 _page 结尾的子路由)
|
||||
return parentRoute.children.filter((child: RouteRecordRaw) => {
|
||||
// 必须有 title
|
||||
if (!child.meta?.title) {
|
||||
return false;
|
||||
}
|
||||
// 排除 path 为空字符串的子路由(代表父路由本身)
|
||||
if (child.path === '' || child.path === undefined) {
|
||||
return false;
|
||||
}
|
||||
// 排除 name 以 _page 结尾的子路由(父路由的默认子路由)
|
||||
if (typeof child.name === 'string' && child.name.endsWith('_page')) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
return [];
|
||||
}
|
||||
Reference in New Issue
Block a user