修复component问题

This commit is contained in:
2026-01-26 15:57:14 +08:00
parent bec473e62a
commit da82e82e7c
5 changed files with 64 additions and 87 deletions
+18 -17
View File
@@ -1,45 +1,46 @@
import { createComponentLoader } from '@/utils/pathResolver';
import { h, resolveComponent as resolveVueComponent } from 'vue';
// 递归转换嵌套菜单为嵌套路由
export function convertMenusToRoutes(menus) {
if (!menus || menus.length === 0) return [];
return menus.map(menu => {
// 基础路由配置
const route = {
path: menu.path || '', // 优先使用菜单path,无则为空字符串
path: menu.path || '',
name: `menu_${menu.id}`,
meta: {
icon: menu.icon,
title: menu.title,
icon: menu.icon,
id: menu.id,
parentId: menu.pid,
menuPath: menu.path,
componentPath: menu.component_path
}
};
// 处理组件路径
// 1. 处理组件加载
if (menu.type === 4) {
// 单页类型:使用单页组件,根据路由从单页表获取内容
// 单页类型
route.component = () => import('@/views/onepage/index.vue');
route.meta.menuType = 4; // 标记为单页类型
} else if (menu.component_path) {
// 页面类型:直接使用component_path加载组件
} else if (menu.component_path && menu.component_path.trim() !== '') {
// 正常页面
route.component = createComponentLoader(menu.component_path);
} else if (menu.children && menu.children.length > 0) {
// 无组件但有子路由的父菜单,添加默认空组件(避免路由报错)
// 目录节点:必须给组件,否则父级无法渲染子级
route.component = () => import('@/views/layouts/EmptyLayout.vue');
// 为父菜单添加重定向,指向第一个有效子路由
const firstValidChild = findFirstValidRoute(menu.children);
if (firstValidChild && firstValidChild.path) {
route.redirect = firstValidChild.path;
}
} else {
// 异常:既没路径也没子菜单
route.component = () => import('@/views/404/404.vue');
}
// 递归处理子菜单为子路由
// 2. 递归子路由
if (menu.children && menu.children.length > 0) {
route.children = convertMenusToRoutes(menu.children);
// 目录节点添加重定向,防止点击父菜单页面空白
const firstChild = menu.children[0];
if (firstChild && firstChild.path) {
route.redirect = firstChild.path;
}
}
return route;