@@ -2,6 +2,7 @@ import type { RouteLocationNormalized, Router, RouteRecordNormalized } from 'vue
2
2
import { createRouter , createWebHashHistory , RouteRecordRaw } from 'vue-router'
3
3
import { isUrl } from '@/utils/is'
4
4
import { cloneDeep , omit } from 'lodash-es'
5
+ import qs from 'qs'
5
6
6
7
const modules = import . meta. glob ( '../views/**/*.{vue,tsx}' )
7
8
/**
@@ -64,6 +65,7 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord
64
65
const res : AppRouteRecordRaw [ ] = [ ]
65
66
const modulesRoutesKeys = Object . keys ( modules )
66
67
for ( const route of routes ) {
68
+ // 1. 生成 meta 菜单元数据
67
69
const meta = {
68
70
title : route . name ,
69
71
icon : route . icon ,
@@ -73,10 +75,20 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord
73
75
route . children &&
74
76
route . children . length === 1 &&
75
77
( route . alwaysShow !== undefined ? route . alwaysShow : true )
78
+ } as any
79
+ // 特殊逻辑:如果后端配置的 MenuDO.component 包含 ?,则表示需要传递参数
80
+ // 此时,我们需要解析参数,并且将参数放到 meta.query 中
81
+ // 这样,后续在 Vue 文件中,可以通过 const { currentRoute } = useRouter() 中,通过 meta.query 获取到参数
82
+ if ( route . component && route . component . indexOf ( '?' ) > - 1 ) {
83
+ const query = route . component . split ( '?' ) [ 1 ]
84
+ route . component = route . component . split ( '?' ) [ 0 ]
85
+ meta . query = qs . parse ( query )
76
86
}
87
+
88
+ // 2. 生成 data(AppRouteRecordRaw)
77
89
// 路由地址转首字母大写驼峰,作为路由名称,适配keepAlive
78
90
let data : AppRouteRecordRaw = {
79
- path : route . path ,
91
+ path : route . path . indexOf ( '?' ) > - 1 ? route . path . split ( '?' ) [ 0 ] : route . path ,
80
92
name :
81
93
route . componentName && route . componentName . length > 0
82
94
? route . componentName
0 commit comments