Skip to content

Commit e24431c

Browse files
committed
refactor!: 移除自定义 Route.recordRaw 类型,改用 RouteRecordRaw 类型
1 parent f85a911 commit e24431c

File tree

6 files changed

+46
-41
lines changed

6 files changed

+46
-41
lines changed

src/global.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,8 @@ declare module 'vue-router' {
225225
}
226226

227227
declare namespace Route {
228-
interface recordRaw extends Omit<RouteRecordRaw, 'meta' | 'children'> {
229-
meta: RouteMeta
230-
children?: recordRaw[]
231-
}
232228
interface recordMainRaw {
233-
meta: {
229+
meta?: {
234230
title?: string | Function
235231
icon?: string
236232
auth?: string | string[]

src/layouts/components/Search/index.vue

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts" setup name="Search">
22
import { cloneDeep } from 'lodash-es'
33
import hotkeys from 'hotkeys-js'
4+
import type { RouteRecordRaw } from 'vue-router'
45
import { isExternalLink } from '@/utils'
56
import eventBus from '@/utils/eventBus'
67
import useSettingsStore from '@/store/modules/settings'
@@ -133,23 +134,23 @@ onMounted(() => {
133134
}
134135
})
135136
136-
function hasChildren(item: Route.recordRaw) {
137+
function hasChildren(item: RouteRecordRaw) {
137138
let flag = true
138-
if (item.children && item.children.every(i => i.meta.sidebar === false)) {
139+
if (item.children?.every(i => i.meta?.sidebar === false)) {
139140
flag = false
140141
}
141142
return flag
142143
}
143-
function getSourceList(arr: Route.recordRaw[], basePath?: string, icon?: string, breadcrumb?: (string | Function | undefined)[]) {
144+
function getSourceList(arr: RouteRecordRaw[], basePath?: string, icon?: string, breadcrumb?: (string | Function | undefined)[]) {
144145
arr.forEach((item) => {
145-
if (item.meta.sidebar !== false) {
146+
if (item.meta?.sidebar !== false) {
146147
const breadcrumbTemp = cloneDeep(breadcrumb) || []
147148
if (item.children && hasChildren(item)) {
148-
breadcrumbTemp.push(item.meta.title)
149-
getSourceList(item.children, basePath ? [basePath, item.path].join('/') : item.path, item.meta.icon ?? icon, breadcrumbTemp)
149+
breadcrumbTemp.push(item.meta?.title)
150+
getSourceList(item.children, basePath ? [basePath, item.path].join('/') : item.path, item.meta?.icon ?? icon, breadcrumbTemp)
150151
}
151152
else {
152-
breadcrumbTemp.push(item.meta.title)
153+
breadcrumbTemp.push(item.meta?.title)
153154
let path = ''
154155
if (isExternalLink(item.path)) {
155156
path = item.path
@@ -158,8 +159,8 @@ function getSourceList(arr: Route.recordRaw[], basePath?: string, icon?: string,
158159
path = basePath ? [basePath, item.path].join('/') : item.path
159160
}
160161
sourceList.value.push({
161-
icon: item.meta.icon ?? icon,
162-
title: item.meta.title,
162+
icon: item.meta?.icon ?? icon,
163+
title: item.meta?.title,
163164
breadcrumb: breadcrumbTemp,
164165
path,
165166
})

src/router/modules/breadcrumb.example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Route } from '@/global'
1+
import type { RouteRecordRaw } from 'vue-router'
22

33
const Layout = () => import('@/layouts/index.vue')
44

5-
const routes: Route.recordRaw = {
5+
const routes: RouteRecordRaw = {
66
path: '/breadcrumb_example',
77
component: Layout,
88
redirect: '/breadcrumb_example/list1',

src/router/modules/multilevel.menu.example.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Route } from '@/global'
1+
import type { RouteRecordRaw } from 'vue-router'
22

33
const Layout = () => import('@/layouts/index.vue')
44

5-
const routes: Route.recordRaw = {
5+
const routes: RouteRecordRaw = {
66
path: '/multilevel_menu_example',
77
component: Layout,
88
redirect: '/multilevel_menu_example/page',

src/router/routes.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { setupLayouts } from 'virtual:generated-layouts'
22
import generatedRoutes from 'virtual:generated-pages'
3+
import type { RouteRecordRaw } from 'vue-router'
34
import MultilevelMenuExample from './modules/multilevel.menu.example'
45
import BreadcrumbExample from './modules/breadcrumb.example'
56
import type { Route } from '@/global'
67
import useSettingsStore from '@/store/modules/settings'
78

89
// 固定路由(默认路由)
9-
const constantRoutes: Route.recordRaw[] = [
10+
const constantRoutes: RouteRecordRaw[] = [
1011
{
1112
path: '/login',
1213
name: 'login',
@@ -26,7 +27,7 @@ const constantRoutes: Route.recordRaw[] = [
2627
]
2728

2829
// 系统路由
29-
const systemRoutes: Route.recordRaw[] = [
30+
const systemRoutes: RouteRecordRaw[] = [
3031
{
3132
path: '/',
3233
component: () => import('@/layouts/index.vue'),
@@ -94,7 +95,7 @@ const constantRoutesByFilesystem = generatedRoutes.filter((item) => {
9495

9596
const asyncRoutesByFilesystem = setupLayouts(generatedRoutes.filter((item) => {
9697
return item.meta?.enabled !== false && item.meta?.constant !== true && item.meta?.layout !== false
97-
})) as Route.recordRaw[]
98+
}))
9899

99100
export {
100101
constantRoutes,

src/store/modules/route.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { cloneDeep } from 'lodash-es'
2+
import type { RouteRecordRaw } from 'vue-router'
23
import useSettingsStore from './settings'
34
import useUserStore from './user'
45
import api from '@/api'
56
import { resolveRoutePath } from '@/utils'
67
import { systemRoutes } from '@/router/routes'
78
import type { Route } from '@/global'
89

9-
function hasPermission(permissions: string[], route: Route.recordMainRaw | Route.recordRaw) {
10+
function hasPermission(permissions: string[], route: Route.recordMainRaw | RouteRecordRaw) {
1011
let isAuth = false
1112
if (route.meta?.auth) {
1213
isAuth = permissions.some((auth) => {
13-
return typeof route.meta.auth === 'string'
14+
return typeof route.meta?.auth === 'string'
1415
? route.meta.auth === auth
15-
: typeof route.meta.auth === 'object'
16+
: typeof route.meta?.auth === 'object'
1617
? route.meta.auth.includes(auth)
1718
: false
1819
})
@@ -23,7 +24,7 @@ function hasPermission(permissions: string[], route: Route.recordMainRaw | Route
2324
return isAuth
2425
}
2526

26-
function filterAsyncRoutes<T extends Route.recordMainRaw[] | Route.recordRaw[]>(routes: T, permissions: string[]): T {
27+
function filterAsyncRoutes<T extends Route.recordMainRaw[] | RouteRecordRaw[]>(routes: T, permissions: string[]): T {
2728
const res: any = []
2829
routes.forEach((route) => {
2930
if (hasPermission(permissions, route)) {
@@ -59,31 +60,34 @@ function formatBackRoutes(routes: any, views = import.meta.glob('../../views/**/
5960
}
6061

6162
// 将多层嵌套路由处理成两层,保留顶层和最子层路由,中间层级将被拍平
62-
function flatAsyncRoutes<T extends Route.recordRaw>(routes: T): T {
63+
function flatAsyncRoutes<T extends RouteRecordRaw>(routes: T): T {
6364
if (routes.children) {
6465
routes.children = flatAsyncRoutesRecursive(routes.children, [{
6566
path: routes.path,
66-
title: routes.meta.title,
67-
hide: !routes.meta.breadcrumb && routes.meta.breadcrumb === false,
67+
title: routes.meta?.title,
68+
hide: !routes.meta?.breadcrumb && routes.meta?.breadcrumb === false,
6869
}], routes.path)
6970
}
7071
return routes
7172
}
72-
function flatAsyncRoutesRecursive(routes: Route.recordRaw[], breadcrumb: Route.breadcrumb[] = [], baseUrl = ''): Route.recordRaw[] {
73-
const res: Route.recordRaw[] = []
73+
function flatAsyncRoutesRecursive(routes: RouteRecordRaw[], breadcrumb: Route.breadcrumb[] = [], baseUrl = ''): RouteRecordRaw[] {
74+
const res: RouteRecordRaw[] = []
7475
routes.forEach((route) => {
7576
if (route.children) {
7677
const childrenBaseUrl = resolveRoutePath(baseUrl, route.path)
7778
const tmpBreadcrumb = cloneDeep(breadcrumb)
78-
if (route.meta.breadcrumb !== false) {
79+
if (route.meta?.breadcrumb !== false) {
7980
tmpBreadcrumb.push({
8081
path: childrenBaseUrl,
81-
title: route.meta.title,
82-
hide: !route.meta.breadcrumb && route.meta.breadcrumb === false,
82+
title: route.meta?.title,
83+
hide: !route.meta?.breadcrumb,
8384
})
8485
}
8586
const tmpRoute = cloneDeep(route)
8687
tmpRoute.path = childrenBaseUrl
88+
if (!tmpRoute.meta) {
89+
tmpRoute.meta = {}
90+
}
8791
tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb
8892
delete tmpRoute.children
8993
res.push(tmpRoute)
@@ -109,9 +113,12 @@ function flatAsyncRoutesRecursive(routes: Route.recordRaw[], breadcrumb: Route.b
109113
const tmpBreadcrumb = cloneDeep(breadcrumb)
110114
tmpBreadcrumb.push({
111115
path: tmpRoute.path,
112-
title: tmpRoute.meta.title,
113-
hide: !tmpRoute.meta.breadcrumb && tmpRoute.meta.breadcrumb === false,
116+
title: tmpRoute.meta?.title,
117+
hide: !tmpRoute.meta?.breadcrumb && tmpRoute.meta?.breadcrumb === false,
114118
})
119+
if (!tmpRoute.meta) {
120+
tmpRoute.meta = {}
121+
}
115122
tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb
116123
res.push(tmpRoute)
117124
}
@@ -126,14 +133,14 @@ const useRouteStore = defineStore(
126133
state: () => ({
127134
isGenerate: false,
128135
routes: [] as Route.recordMainRaw[],
129-
fileSystemRoutes: [] as Route.recordRaw[],
136+
fileSystemRoutes: [] as RouteRecordRaw[],
130137
currentRemoveRoutes: [] as Function[],
131138
}),
132139
getters: {
133140
// 扁平化路由(将三级及以上路由数据拍平成二级)
134141
flatRoutes: (state) => {
135142
const settingsStore = useSettingsStore()
136-
const routes: Route.recordRaw[] = []
143+
const routes: RouteRecordRaw[] = []
137144
if (state.routes) {
138145
if (settingsStore.app.routeBaseOn !== 'filesystem') {
139146
state.routes.forEach((item) => {
@@ -171,7 +178,7 @@ const useRouteStore = defineStore(
171178
}
172179
// 设置 routes 数据
173180
this.isGenerate = true
174-
this.routes = accessedRoutes.filter(item => item.children?.length !== 0)
181+
this.routes = accessedRoutes.filter(item => item.children?.length !== 0) as any
175182
resolve()
176183
})
177184
},
@@ -195,13 +202,13 @@ const useRouteStore = defineStore(
195202
}
196203
// 设置 routes 数据
197204
this.isGenerate = true
198-
this.routes = accessedRoutes.filter(item => item.children.length !== 0)
205+
this.routes = accessedRoutes.filter(item => item.children.length !== 0) as any
199206
resolve()
200207
})
201208
})
202209
},
203210
// 根据权限动态生成路由(文件系统生成)
204-
generateRoutesAtFilesystem(asyncRoutes: Route.recordRaw[]) {
211+
generateRoutesAtFilesystem(asyncRoutes: RouteRecordRaw[]) {
205212
// eslint-disable-next-line no-async-promise-executor
206213
return new Promise<void>(async (resolve) => {
207214
const settingsStore = useSettingsStore()
@@ -217,7 +224,7 @@ const useRouteStore = defineStore(
217224
}
218225
// 设置 routes 数据
219226
this.isGenerate = true
220-
this.fileSystemRoutes = accessedRoutes.filter(item => item.children?.length !== 0)
227+
this.fileSystemRoutes = accessedRoutes.filter(item => item.children?.length !== 0) as any
221228
resolve()
222229
})
223230
},

0 commit comments

Comments
 (0)