解决vue动态路由异步加载import组件,加载不到module的问题
作者:Cookysurongbin 时间:2024-05-29 22:45:37
报错信息应该是这样的
webpackEmptyContext (eval at ./src/store/modules sync recursive (0.js:10), <anonymous>:2:10)
路由信息由后端给出,那么前端需要动态加载路由,同时component的路径也是后端给出,但是动态加载该路径会报错
如:
// 假如path = '@/views/user'
const com = () => import(path) // 这样会报错哦
const com2 = () = > import('@/views/user') // 这样写死的字符串就可以
原因应该是在webpack,webpack 编译es6 动态引入 import() 时不能传入变量,因为webpack的现在的实现方式不能实现完全动态。
解决办法:
可以通过字符串模板来提供部分信息给webpack,例如import(`@/${path}`), 这样编译时会编译所有@/views下的模块,但运行时确定path的值才会加载,从而实现懒加载。
如果写了@给webpack还是不能识别,那么可能你需要多些几级的路径,如import(`@/views/${path}`)
补充知识:vue中使用import路由懒加载报错解决方法
一般情况下都会正常运行,但当我们运行npm run dev时却报错
原因是import属于异步引用组件,需要babel-loader处理
所以我们需要安装
npm install babel-plugin-syntax-dynamic-import -D
这是用来编译import异步引用方法的模块
然后在.babelrc中引入这个插件
{
“plugin”: [‘syntax-dynamic-import']
}
来源:https://blog.csdn.net/Cookysurongbin/article/details/99307933
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微信小程序实现点击导航标签滚动定位到对应位置
![](https://img.aspxhome.com/file/2023/4/127194_0s.gif)
python中列表的常见操作梳理总结(一)
![](https://img.aspxhome.com/file/2023/9/135089_0s.png)
Python绘制组合图的示例
![](https://img.aspxhome.com/file/2023/0/128210_0s.png)
Go语言中的数据竞争模式详解
![](https://img.aspxhome.com/file/2023/9/123909_0s.png)
jQuery ajaxSubmit 实现ajax提交表单局部刷新
基于python实现高速视频传输程序
Python多线程结合队列下载百度音乐的方法
ASP存储过程开发应用详解第1/2页
python 简单的股票基金爬虫
![](https://img.aspxhome.com/file/2023/4/125054_0s.png)
python MysqlDb模块安装及其使用详解
![](https://img.aspxhome.com/file/2023/8/97228_0s.png)
一文带你上手Vue新的状态管理Pinia
![](https://img.aspxhome.com/file/2023/0/126560_0s.png)
python中*args与**kwarsg及闭包和装饰器的用法
![](https://img.aspxhome.com/file/2023/6/60956_0s.png)
Python 的可变和不可变对象详情
![](https://img.aspxhome.com/file/2023/2/87642_0s.png)
bootstrap-table实现表头固定以及列固定的方法示例
![](https://img.aspxhome.com/file/2023/0/132830_0s.png)
python的Template使用指南
浅析SQL Server中包含事务的存储过程
golang gorm 结构体的表字段缺省值设置方式
Go语言中defer语句的用法
![](https://img.aspxhome.com/file/2023/3/92023_0s.png)
Python通过wordcloud库实现将单词生成词云
![](https://img.aspxhome.com/file/2023/3/83843_0s.png)