From a9e3bb93d37c809bd21e3c13b51d5527dab72832 Mon Sep 17 00:00:00 2001 From: buaixuexideshitongxue <2936013465@qq.com> Date: Tue, 3 Feb 2026 19:33:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=AE=E5=BD=95=E8=B0=83=E6=95=B4--=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=B7=BB=E5=8A=A0=E5=A4=96=E9=93=BE=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/permission.ts | 20 +++++++++++++++----- src/router/index.ts | 27 ++++++++++++++++++++------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/permission.ts b/src/permission.ts index 7584ebb..006266c 100644 --- a/src/permission.ts +++ b/src/permission.ts @@ -40,14 +40,24 @@ router.beforeEach(async (to, from, next) => { router.addRoute(INDEX_ROUTE) routes.forEach((route: any) => { // 动态添加可访问路由表 - if (!route.children) { - if (route.meta.type === MenuEnum.CATALOGUE) { + try { + if (!route) { return } - router.addRoute(INDEX_ROUTE_NAME, route) - return + if (!route.path || !route.name) { + return + } + if (!route.children) { + if (route.meta?.type === MenuEnum.CATALOGUE) { + return + } + router.addRoute(INDEX_ROUTE_NAME, route) + return + } + router.addRoute(route) + } catch (error) { + console.error('添加路由失败:', error) } - router.addRoute(route) }) next({ ...to, replace: true }) return diff --git a/src/router/index.ts b/src/router/index.ts index d565c51..7df566c 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -12,18 +12,21 @@ export function getModulesKey() { } export function createRouteRecord(menus) { - return menus.map(menu => { + // 过滤掉null或undefined的菜单项 + const validMenus = menus.filter(menu => menu != null) + + return validMenus.map(menu => { const routeRecord: RouteRecordRaw = { - path: menu.paths, - name: Symbol(menu.paths), + path: menu.paths || '/', + name: Symbol(menu.paths || Math.random().toString()), meta: { hidden: !menu.isShow, keepAlive: !!menu.isCache, - title: menu.menuName, + title: menu.menuName || '', perms: menu.perms, icon: menu.icon, type: menu.menuType, - openOutside:menu.openOutside, + openOutside: menu.openOutside, openNewPage: menu.openNewPage } } @@ -32,10 +35,13 @@ export function createRouteRecord(menus) { routeRecord.component = LAYOUT break case MenuEnum.MENU: - routeRecord.component = loadRouteView(menu.component) + routeRecord.component = loadRouteView(menu.component || '') + break + default: + routeRecord.component = RouterView break } - if (menu.children.length) { + if (menu.children && menu.children.length) { routeRecord.children = createRouteRecord(menu.children) } return routeRecord @@ -45,6 +51,13 @@ export function createRouteRecord(menus) { // 动态加载组件 function loadRouteView(component: string) { try { + // 检查是否是URL类型的组件路径 + if (component.startsWith('http://') || component.startsWith('https://')) { + // 对于URL类型的组件,返回RouterView作为占位符 + // 实际的URL打开会通过openOutside或openNewPage处理 + return RouterView + } + const key = Object.keys(modules).find((key) => { return key.includes(`${component}.vue`) })