上一篇文章中的主要讲的是只有一级菜单的情况,当出现需要需要做二级菜单权限的时候,则需要这样改,此时后端传递过来的数据格式为:
{
'user_router': [{
'component': 'Layout',
'icon': 'form',
'id': 15,
'name': 'alipay1',
'path': '/alipay1',
'title': '一级菜单1',
'uid': 5,
'children': [{
'component': '/equipmentManage/equipmentRefresh',
'icon': 'form',
'id': 16,
'name': 'alipay2',
'path': 'alipay2',
'title': '二级菜单1',
'children': []
}]
}, {
'component': 'Layout',
'icon': 'form',
'id': 15,
'name': 'alipay2',
'path': '/alipay2',
'title': '一级菜单2',
'uid': 5,
'children': [{
'component': '/equipmentManage/equipmentRefresh',
'icon': 'form',
'id': 16,
'name': 'alipay2',
'path': 'alipay2',
'title': '二级菜单2',
'children': []
}]
}
]
}
修改format函数为:
export function formatRouterFunc(routers) {
const formatRouters = []
routers.forEach(router => {
const path = router.path
const name = router.name
const title = router.title
const component = router.component
const icon = router.icon
let children = router.children
if (children && children instanceof Array) {
children = formatRouterFunc(children)
}
const formatRouter = {
path: path,
component(resolve) {
if (router.component === 'Layout') {
require(['@/layout'], resolve)
// Layout 尝试过不行
} else {
require(['@/views' + component], resolve)
}
},
name: name,
meta: {
title: title,
icon: icon
},
children: children
}
formatRouters.push(formatRouter)
})
return formatRouters
}
只需改动以上代码即可
参考资料:
http://www.devopser.org/articles/2019/04/29/1556524856420.html