import { RouteRecordRaw, RouteLocationNormalized } from 'vue-router'; export function getParentChildrenRoutes(route: RouteLocationNormalized): RouteRecordRaw[] { // 没有任何匹配路由,直接返回空 if (!route.matched || route.matched.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; } } return []; }