首页 > Web开发 > 详细

vue-router 根据权限动态设置路由 theme.js" as it exceeds the max of "500KB".

时间:2019-10-24 03:05:46      阅读:1233      评论:0      收藏:0      [点我收藏+]

需求: 根据不同角色的登录人,展示不同的功能模块. 前端路由在后台维护,动态注册路由.

流程:

  首先,登录成功,获取token

  其次,通过在请求头携带当前登录人的token,调取module的接口

axios(‘module.list‘).then(res => {
  if (res.data.status === 200) {
    this.moduleList = res.data.res;
  }  
})

 

  接着,处理数据格式,主要是component的格式是,例如: import(`@view/user/${item.path}`)

    

this.newAddRouter = this.moduleList.map(item => {
    return {
       code: item.code,
    icon: item.iconUrl,
    name: item.routerPath,
    path: item.routerPath,
    title: item.name,
    id: item.id,
    component: () => import(`@/views/tv/tv-nine/${item.path}`), // 正确的写法
   // component: () => import(`${item.path}`), // 报错 theme.js" as it exceeds the max of "500KB".
  
    parentId: item.parentId,
    children: []
  
  };
});

此处省略递归处理

  

  最后,通过this.addroutes()注册动态路由  

this.$router.addroutes(newAddRouter);
注: 由于我们的需求是在登录成功后才根据登录人的角色动态生成模块,而router是在vue实例化的时候就已经注册挂载,所以,官方提供了一个动态的注册的api,它就是addroutes.

  

  

vue-router 根据权限动态设置路由 theme.js" as it exceeds the max of "500KB".

原文:https://www.cnblogs.com/web-zqk/p/11729550.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!