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 @@
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 { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'
import { cloneDeep, omit } from 'lodash' import { cloneDeep, omit } from 'lodash'
import qs from 'qs' import qs from 'qs'
@ -77,7 +82,7 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord
alwaysShow: alwaysShow:
route.children && route.children &&
route.children.length === 1 && route.children.length === 1 &&
(route.alwaysShow !== undefined ? route.alwaysShow : true) (route.alwaysShow !== undefined ? route.alwaysShow : true),
} as any } as any
// 特殊逻辑:如果后端配置的 MenuDO.component 包含 ?,则表示需要传递参数 // 特殊逻辑:如果后端配置的 MenuDO.component 包含 ?,则表示需要传递参数
// 此时,我们需要解析参数,并且将参数放到 meta.query 中 // 此时,我们需要解析参数,并且将参数放到 meta.query 中
@ -92,12 +97,13 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord
// 路由地址转首字母大写驼峰,作为路由名称,适配keepAlive // 路由地址转首字母大写驼峰,作为路由名称,适配keepAlive
let data: AppRouteRecordRaw = { let data: AppRouteRecordRaw = {
path: route.path.indexOf('?') > -1 ? route.path.split('?')[0] : route.path, path: route.path.indexOf('?') > -1 ? route.path.split('?')[0] : route.path,
name: name: route.componentName
route.componentName && route.componentName.length > 0 ? route.componentName.length > 0
? route.componentName ? route.componentName
: toCamelCase(route.path, true), : toCamelCase(route.path, true)
: '',
redirect: route.redirect, redirect: route.redirect,
meta: meta meta: meta,
} }
//处理顶级非目录路由 //处理顶级非目录路由
if (!route.children && route.parentId == 0 && route.component) { if (!route.children && route.parentId == 0 && route.component) {
@ -108,16 +114,17 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord
meta.alwaysShow = true meta.alwaysShow = true
const childrenData: AppRouteRecordRaw = { const childrenData: AppRouteRecordRaw = {
path: '', path: '',
name: name: route.componentName
route.componentName && route.componentName.length > 0 ? route.componentName.length > 0
? route.componentName ? route.componentName
: toCamelCase(route.path, true), : toCamelCase(route.path, true)
: '',
redirect: route.redirect, redirect: route.redirect,
meta: meta meta: meta,
} }
const index = route?.component const index = route?.component
? modulesRoutesKeys.findIndex((ev) => ev.includes(route.component)) ? modulesRoutesKeys.findIndex(ev => ev.includes(route.component))
: modulesRoutesKeys.findIndex((ev) => ev.includes(route.path)) : modulesRoutesKeys.findIndex(ev => ev.includes(route.path))
childrenData.component = modules[modulesRoutesKeys[index]] childrenData.component = modules[modulesRoutesKeys[index]]
data.children = [childrenData] data.children = [childrenData]
} else { } else {
@ -131,16 +138,16 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord
path: '/external-link', path: '/external-link',
component: Layout, component: Layout,
meta: { meta: {
name: route.name name: route.name,
}, },
children: [data] children: [data],
} as AppRouteRecordRaw } as AppRouteRecordRaw
// 菜单 // 菜单
} else { } else {
// 对后端传component组件路径和不传做兼容(如果后端传component组件路径,那么path可以随便写,如果不传,component组件路径会根path保持一致) // 对后端传component组件路径和不传做兼容(如果后端传component组件路径,那么path可以随便写,如果不传,component组件路径会根path保持一致)
const index = route?.component const index = route?.component
? modulesRoutesKeys.findIndex((ev) => ev.includes(route.component)) ? modulesRoutesKeys.findIndex(ev => ev.includes(route.component))
: modulesRoutesKeys.findIndex((ev) => ev.includes(route.path)) : modulesRoutesKeys.findIndex(ev => ev.includes(route.path))
data.component = modules[modulesRoutesKeys[index]] data.component = modules[modulesRoutesKeys[index]]
} }
if (route.children) { if (route.children) {

Loading…
Cancel
Save