为了账号安全,请及时绑定邮箱和手机立即绑定

vue 通过json数据如何找到对应组件然后 addRouter 添加路由?

vue 通过json数据如何找到对应组件然后 addRouter 添加路由?

月关宝盒 2019-03-14 18:19:50
通过后端返回数据然后找到对应组件 addRouter 实现用户路由权限,在动态加载组件时,一直报错,找不到组件;相关代码actions: {GenerateRoutes({ commit, rootState }) {  return new Promise(resolve => {    let accessedRouters = [];        let menuList = rootState.user.menu;        menuList.forEach(function(item,index,arry){            item.component = Layout;            if(item.children&&item.children.length>=1){                item.children.forEach(function(chItem, chIndex,chArry){                    let path = chArry[0].path;                    if(typeof chItem.component=='string'){                            chItem.component = resolve => require([`../../views/` + chItem.component + `.vue`], resolve);                    };                    item["redirect"] = item.path+"/"+path;                    item["meta"] = { "title": item.title,"icon": item.icon};                    chItem["meta"] = { "title": chItem.title}                });            };                    })        accessedRouters = menuList;        accessedRouters.push({                    path: '*',                    redirect: '/404',                    hidden: true                });        commit('SET_ROUTERS', accessedRouters)    resolve()  })}}后来把组件名字用变量接收下再拼接就可以了,具体原因还在研究,有碰到这个问题的多注意一下,够坑的;let comName = chItem.component;if(typeof chItem.component=='string'){    chItem.component = resolve => require([`../../views` + comName + `.vue`], resolve); };
查看完整描述

2 回答

?
千万里不及你

TA贡献1784条经验 获得超9个赞

let comName = chItem.component;
if(typeof chItem.component=='string'){

chItem.component = resolve => require([`../../views` + comName + `.vue`], resolve);

};


查看完整回答
反对 回复 2019-04-10
?
GCT1015

TA贡献1827条经验 获得超4个赞

有完整的demo吗?为什么我的addRoutes一直不生效呢?


查看完整回答
反对 回复 2019-04-10
  • 2 回答
  • 0 关注
  • 1104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号