Vue-element-admin: 从数据库查到数据后, 动态加载路由, 报Cannot find module

Created on 28 Sep 2018  ·  7Comments  ·  Source: PanJiaChen/vue-element-admin

大神您好,我 从后台数据库查寻数据生成路由表,菜单列表,权限都能出来,但是在路由异步加载的时报Cannot find module '@/views/layout/layout'
希望大神您能帮我看一下问题出在了哪里
我是将路径@/views/layout/layout 用varchar格式存入mysql 数据库 将数据查询到页面后,
我创建了变量来接受路由信息
let newRouter =[
{ path: data.path,
component: () => import(data.component),
name:data.name
}
]
变量创建完毕后,执行了
accessedRouters = filterAsyncRouter(nr, roles)
commit('SET_ROUTERS', accessedRouters)
成功生成addRouters

页面 可以 成功显示出 对应的菜单, 但是 点击菜单进行路由时,就会报Cannot find module ‘@/views/layout/layout ’ 这个错误,

我把上面的 newRouter 变量的 component 直接写成 () => import(‘@/views/layout/layout ’ ) 是不会报任何的错误,
想问一下 大神 () => import(data.component) 如何才能成功路由

Most helpful comment

完美解决。
route.component = loadView(route.component)

export const loadView = (view) => { // 路由懒加载
return (resolve) => require([@/views/${view}], resolve)
}
解决方案来自:
https://gitee.com/y_project/RuoYi-Vue/blob/da5c1328beb06a4c785aa2791a2b9ca3bbeefaa6/ruoyi-ui/src/store/modules/permission.js

All 7 comments

@moxuanluo 问题解决了么,
我用 () => import('@/views/path')这种方式加载接口返回的路由,点击后提示 Failed to mount component: template or render function not defined.

请问下载这个问题解决了吗

我同样遇到这个问题,久思未果,如果问题有结果请麻烦告知

我同样遇到这个问题,久思未果,如果问题有结果请麻烦告知
我是这样解决的,本地缓存一份路由表。
image
image

同遇到这个问题。。直接写就没问题

完美解决。
route.component = loadView(route.component)

export const loadView = (view) => { // 路由懒加载
return (resolve) => require([@/views/${view}], resolve)
}
解决方案来自:
https://gitee.com/y_project/RuoYi-Vue/blob/da5c1328beb06a4c785aa2791a2b9ca3bbeefaa6/ruoyi-ui/src/store/modules/permission.js

Was this page helpful?
0 / 5 - 0 ratings