|
|
@@ -30,20 +30,29 @@ export function resolvePath(basePath: string, routePath: string): string {
|
|
|
* 将嵌套菜单数据扁平化为 Vue Router 可用的路由配置
|
|
|
* 所有动态路由最终挂载在 Root(Layout)下
|
|
|
*/
|
|
|
-export function flattenMenuToRoutes(
|
|
|
- menuItems: MenuItem[],
|
|
|
- parentPath = ''
|
|
|
-): RouteRecordRaw[] {
|
|
|
+export function flattenMenuToRoutes(menuItems: MenuItem[], parentPath = ''): RouteRecordRaw[] {
|
|
|
const routes: RouteRecordRaw[] = []
|
|
|
|
|
|
menuItems.forEach(item => {
|
|
|
const fullPath = resolvePath(parentPath, item.path)
|
|
|
|
|
|
- if (item.children && item.children.length > 0) {
|
|
|
+ // 过滤掉 hidden 的子菜单
|
|
|
+ const visibleChildren = item.children?.filter(child => !child.meta?.hidden) || []
|
|
|
+
|
|
|
+ if (visibleChildren.length > 0) {
|
|
|
// 父级菜单:注册 redirect 路由,再递归处理子项
|
|
|
routes.push({
|
|
|
path: fullPath,
|
|
|
- redirect: item.redirect || resolvePath(fullPath, item.children[0].path),
|
|
|
+ redirect: item.redirect || resolvePath(fullPath, visibleChildren[0].path),
|
|
|
+ meta: item.meta
|
|
|
+ } as RouteRecordRaw)
|
|
|
+ routes.push(...flattenMenuToRoutes(visibleChildren, fullPath))
|
|
|
+ } else if (item.children && item.children.length > 0) {
|
|
|
+ // 有子菜单但都是隐藏的:注册父级路由 + 递归处理隐藏子项
|
|
|
+ routes.push({
|
|
|
+ path: fullPath,
|
|
|
+ name: item.name,
|
|
|
+ component: loadComponent(item.component!),
|
|
|
meta: item.meta
|
|
|
} as RouteRecordRaw)
|
|
|
routes.push(...flattenMenuToRoutes(item.children, fullPath))
|