generateRoute.js
1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* @Date: 2022-05-16 17:21:45
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-06-29 06:19:41
* @FilePath: /tswj/src/utils/generateRoute.js
* @Description: 文件描述
*/
/**
* 生成动态路由
*/
import asyncRoutes from "../mock/routes"
// 根据后台返回的路径,生成页面的组件模版
function loadView(component) {
return () => import(`../views/${component}.vue`)
}
const formatRoutesArr = []
asyncRoutes.forEach(route => {
const router = {}
const {
path,
redirect,
name,
component,
keepAlive,
meta,
children
} = route
router.path = path
redirect && (router.redirect = redirect)
name && (router.name = name)
router.component = loadView(component)
keepAlive && (router.keepAlive = keepAlive)
meta && (router.meta = meta)
if (children && children instanceof Array && children.length > 0) {
router.children = formatRoutes(children)
}
formatRoutesArr.push(router)
})
export const generateRoute = (to) => {
let router = ''
formatRoutesArr.forEach(item => {
if (item.path === to.path) {
router = item
}
})
return { ...to, ...router }
}