Skip to content

Commit f75e8d1

Browse files
committed
【新增】在菜单管理中,可通过“路由地址”添加 ? 拼接参数
1 parent bde5597 commit f75e8d1

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/utils/routerHelper.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { RouteLocationNormalized, Router, RouteRecordNormalized } from 'vue
22
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
33
import { isUrl } from '@/utils/is'
44
import { cloneDeep, omit } from 'lodash-es'
5+
import qs from 'qs'
56

67
const modules = import.meta.glob('../views/**/*.{vue,tsx}')
78
/**
@@ -64,6 +65,7 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord
6465
const res: AppRouteRecordRaw[] = []
6566
const modulesRoutesKeys = Object.keys(modules)
6667
for (const route of routes) {
68+
// 1. 生成 meta 菜单元数据
6769
const meta = {
6870
title: route.name,
6971
icon: route.icon,
@@ -73,10 +75,20 @@ export const generateRoute = (routes: AppCustomRouteRecordRaw[]): AppRouteRecord
7375
route.children &&
7476
route.children.length === 1 &&
7577
(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)
7686
}
87+
88+
// 2. 生成 data(AppRouteRecordRaw)
7789
// 路由地址转首字母大写驼峰,作为路由名称,适配keepAlive
7890
let data: AppRouteRecordRaw = {
79-
path: route.path,
91+
path: route.path.indexOf('?') > -1 ? route.path.split('?')[0] : route.path,
8092
name:
8193
route.componentName && route.componentName.length > 0
8294
? route.componentName

0 commit comments

Comments
 (0)