element-admin通过后端动态路由表实现用户权限控制(二)

上一篇文章中的主要讲的是只有一级菜单的情况,当出现需要需要做二级菜单权限的时候,则需要这样改,此时后端传递过来的数据格式为:

{
            '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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值