From 29dd251f5884346fa841ba517a1573fff850bda5 Mon Sep 17 00:00:00 2001 From: wangqi <3188864257@qq.com> Date: Thu, 26 Dec 2024 14:34:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B7=AF=E7=94=B1=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/routerHelper.ts | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/utils/routerHelper.ts b/src/utils/routerHelper.ts index a47b0c6..b5b913a 100644 --- a/src/utils/routerHelper.ts +++ b/src/utils/routerHelper.ts @@ -1,4 +1,9 @@ -import type { RouteLocationNormalized, RouteMeta, Router, RouteRecordNormalized } from 'vue-router' +import type { + RouteLocationNormalized, + RouteMeta, + Router, + RouteRecordNormalized, +} from 'vue-router' import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router' import { cloneDeep, omit } from 'lodash' import qs from 'qs' @@ -77,7 +82,7 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord alwaysShow: route.children && route.children.length === 1 && - (route.alwaysShow !== undefined ? route.alwaysShow : true) + (route.alwaysShow !== undefined ? route.alwaysShow : true), } as any // 特殊逻辑:如果后端配置的 MenuDO.component 包含 ?,则表示需要传递参数 // 此时,我们需要解析参数,并且将参数放到 meta.query 中 @@ -92,12 +97,13 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord // 路由地址转首字母大写驼峰,作为路由名称,适配keepAlive let data: AppRouteRecordRaw = { path: route.path.indexOf('?') > -1 ? route.path.split('?')[0] : route.path, - name: - route.componentName && route.componentName.length > 0 + name: route.componentName + ? route.componentName.length > 0 ? route.componentName - : toCamelCase(route.path, true), + : toCamelCase(route.path, true) + : '', redirect: route.redirect, - meta: meta + meta: meta, } //处理顶级非目录路由 if (!route.children && route.parentId == 0 && route.component) { @@ -108,16 +114,17 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord meta.alwaysShow = true const childrenData: AppRouteRecordRaw = { path: '', - name: - route.componentName && route.componentName.length > 0 + name: route.componentName + ? route.componentName.length > 0 ? route.componentName - : toCamelCase(route.path, true), + : toCamelCase(route.path, true) + : '', redirect: route.redirect, - meta: meta + meta: meta, } const index = route?.component - ? modulesRoutesKeys.findIndex((ev) => ev.includes(route.component)) - : modulesRoutesKeys.findIndex((ev) => ev.includes(route.path)) + ? modulesRoutesKeys.findIndex(ev => ev.includes(route.component)) + : modulesRoutesKeys.findIndex(ev => ev.includes(route.path)) childrenData.component = modules[modulesRoutesKeys[index]] data.children = [childrenData] } else { @@ -131,16 +138,16 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord path: '/external-link', component: Layout, meta: { - name: route.name + name: route.name, }, - children: [data] + children: [data], } as AppRouteRecordRaw // 菜单 } else { // 对后端传component组件路径和不传做兼容(如果后端传component组件路径,那么path可以随便写,如果不传,component组件路径会根path保持一致) const index = route?.component - ? modulesRoutesKeys.findIndex((ev) => ev.includes(route.component)) - : modulesRoutesKeys.findIndex((ev) => ev.includes(route.path)) + ? modulesRoutesKeys.findIndex(ev => ev.includes(route.component)) + : modulesRoutesKeys.findIndex(ev => ev.includes(route.path)) data.component = modules[modulesRoutesKeys[index]] } if (route.children) {