Browse Source

fix: 路由逻辑调整

master
wangqi 2 months ago
parent
commit
29dd251f58
  1. 39
      src/utils/routerHelper.ts

39
src/utils/routerHelper.ts

@ -1,4 +1,9 @@ @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) {

Loading…
Cancel
Save