Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Hooke
/
tswj
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
hookehuyr
2022-06-29 16:06:23 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3e1ad7cfb7cd80a9dc32eedca644f994f6dfd42c
3e1ad7cf
1 parent
07c4e49e
路由生成写法调整
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
10 deletions
src/utils/generateRoute.js
src/utils/generateRoute.js
View file @
3e1ad7c
/*
* @Date: 2022-05-16 17:21:45
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-06-29
06:19:41
* @LastEditTime: 2022-06-29
16:00:23
* @FilePath: /tswj/src/utils/generateRoute.js
* @Description: 文件描述
*/
/**
* 生成动态路由
*/
import
asyncRoutes
from
"../mock/routes"
// 根据后台返回的路径,生成页面的组件模版
/**
* 根据后台返回的路径,生成页面的组件模版
* @param {*} component
* @returns 模版地址
*/
function
loadView
(
component
)
{
return
()
=>
import
(
`../views/
${
component
}
.vue`
)
}
const
formatRoutesArr
=
[]
asyncRoutes
.
forEach
(
route
=>
{
/**
* 生成路由结构
* @param {*} routes 动态获取数据
* @description 动态路由没有嵌套结构,children被拉平,都是平级的结构
*/
const
formatRoutes
=
(
routes
)
=>
{
routes
.
forEach
(
route
=>
{
const
router
=
{}
const
{
path
,
...
...
@@ -35,13 +42,19 @@ asyncRoutes.forEach(route => {
router
.
component
=
loadView
(
component
)
keepAlive
&&
(
router
.
keepAlive
=
keepAlive
)
meta
&&
(
router
.
meta
=
meta
)
if
(
children
&&
children
instanceof
Array
&&
children
.
length
>
0
)
{
router
.
children
=
formatRoutes
(
children
)
}
router
.
children
=
!
Array
.
isArray
(
children
)
||
formatRoutes
(
children
);
formatRoutesArr
.
push
(
router
)
})
})
}
formatRoutes
(
asyncRoutes
)
/**
* 生成动态路由
* @param {*} to
* @returns 符合路径条件的动态路由结构
*/
export
const
generateRoute
=
(
to
)
=>
{
let
router
=
''
formatRoutesArr
.
forEach
(
item
=>
{
...
...
Please
register
or
login
to post a comment