import { RouteRecordRaw, RouteLocationNormalized } from 'vue-router'; export function getParentChildrenRoutes(route: RouteLocationNormalized): RouteRecordRaw[] { // 判断是否有父节点(至少需要2个匹配的路由) if (route.matched.length < 2) { return []; } // 获取倒数第二个匹配的路由(父路由) const parentRoute = route.matched[route.matched.length - 2]; // 检查父路由是否有子路由 if (!parentRoute?.children || parentRoute.children.length === 0) { return []; } // 返回有 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; }); }