Skip to content

Commit 04fa39c

Browse files
committed
feat: 路由增加 meta.menu 配置,同时 meta.sidebar 进入弃用过渡期
1 parent 1685f8b commit 04fa39c

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

src/layouts/components/Menu/sub.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const transitionClass = computed(() => {
9696
const hasChildren = computed(() => {
9797
let flag = true
9898
if (props.menu.children) {
99-
if (props.menu.children.every((item: any) => item.meta?.sidebar === false)) {
99+
if (props.menu.children.every((item: any) => item.meta?.menu === false)) {
100100
flag = false
101101
}
102102
}
@@ -189,7 +189,7 @@ function handleMouseleave() {
189189
}"
190190
>
191191
<template v-for="item in menu.children" :key="item.path ?? JSON.stringify(item)">
192-
<SubMenu v-if="item.meta?.sidebar !== false" :unique-key="[...uniqueKey, item.path ?? JSON.stringify(item)]" :menu="item" :level="level + 1" />
192+
<SubMenu v-if="item.meta?.menu !== false" :unique-key="[...uniqueKey, item.path ?? JSON.stringify(item)]" :menu="item" :level="level + 1" />
193193
</template>
194194
</OverlayScrollbarsComponent>
195195
</Transition>

src/layouts/components/Search/index.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ function initSourceList() {
163163
164164
function hasChildren(item: RouteRecordRaw) {
165165
let flag = true
166-
if (item.children?.every(i => i.meta?.sidebar === false)) {
166+
if (item.children?.every(i => i.meta?.menu === false)) {
167167
flag = false
168168
}
169169
return flag
170170
}
171171
function getSourceList(arr: RouteRecordRaw[], basePath?: string, icon?: string, breadcrumb?: { title?: string | (() => string) }[]) {
172172
arr.forEach((item) => {
173-
if (item.meta?.sidebar !== false) {
173+
if (item.meta?.menu !== false) {
174174
const breadcrumbTemp = cloneDeep(breadcrumb) || []
175175
if (item.children && hasChildren(item)) {
176176
breadcrumbTemp.push({

src/store/modules/menu.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const useMenuStore = defineStore(
7171
function getDeepestPath(menu: Menu.recordRaw, rootPath = '') {
7272
let retnPath = ''
7373
if (menu.children) {
74-
const item = menu.children.find(item => item.meta?.sidebar !== false)
74+
const item = menu.children.find(item => item.meta?.menu !== false)
7575
if (item) {
7676
retnPath = getDeepestPath(item, resolveRoutePath(rootPath, menu.path))
7777
}

src/store/modules/route.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,37 @@ const useRouteStore = defineStore(
169169
return returnRoutes
170170
})
171171

172+
// TODO 将设置 meta.sidebar 的属性转换成 meta.menu ,过渡处理,未来将被弃用
173+
let isUsedDeprecatedAttribute = false
174+
function converDeprecatedAttribute<T extends Route.recordMainRaw[]>(routes: T): T {
175+
routes.forEach((route) => {
176+
route.children = converDeprecatedAttributeRecursive(route.children)
177+
})
178+
if (isUsedDeprecatedAttribute) {
179+
console.warn('[Fantastic-admin] 路由配置中的 "sidebar" 属性即将被弃用, 请尽快替换为 "menu" 属性')
180+
}
181+
return routes
182+
}
183+
function converDeprecatedAttributeRecursive(routes: RouteRecordRaw[]) {
184+
if (routes) {
185+
routes.forEach((route) => {
186+
if (typeof route.meta?.sidebar === 'boolean') {
187+
isUsedDeprecatedAttribute = true
188+
route.meta.menu = route.meta.sidebar
189+
delete route.meta.sidebar
190+
}
191+
if (route.children) {
192+
converDeprecatedAttributeRecursive(route.children)
193+
}
194+
})
195+
}
196+
return routes
197+
}
198+
172199
// 根据权限动态生成路由(前端生成)
173200
async function generateRoutesAtFront(asyncRoutes: Route.recordMainRaw[]) {
174201
// 设置 routes 数据
175-
routesRaw.value = cloneDeep(asyncRoutes) as any
202+
routesRaw.value = converDeprecatedAttribute(cloneDeep(asyncRoutes) as any)
176203
if (settingsStore.settings.app.enablePermission) {
177204
await userStore.getPermissions()
178205
}
@@ -203,7 +230,7 @@ const useRouteStore = defineStore(
203230
async function generateRoutesAtBack() {
204231
await apiApp.routeList().then(async (res) => {
205232
// 设置 routes 数据
206-
routesRaw.value = formatBackRoutes(res.data) as any
233+
routesRaw.value = converDeprecatedAttribute(formatBackRoutes(res.data) as any)
207234
if (settingsStore.settings.app.enablePermission) {
208235
await userStore.getPermissions()
209236
}

src/types/global.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ declare module 'vue-router' {
228228
defaultOpened?: boolean
229229
auth?: string | string[]
230230
sidebar?: boolean
231+
menu?: boolean
231232
breadcrumb?: boolean
232233
activeMenu?: string
233234
cache?: boolean | string | string[]
@@ -263,7 +264,7 @@ declare namespace Menu {
263264
icon?: string
264265
defaultOpened?: boolean
265266
auth?: string | string[]
266-
sidebar?: boolean
267+
menu?: boolean
267268
link?: string
268269
}
269270
children?: recordRaw[]

0 commit comments

Comments
 (0)