[b]发现问题[/b]
运行一下以前的一个Vue+webpack的 vue仿新闻网站 小项目,报错
由于自己vue学习不深入,老是这个报错,找了好久(确切的说是整整一下午^...^)才找到原因 -v-
[code]Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'[/code]
[img]http://files.jb51.net/file_images/article/201706/2017617142541750.png?2017517142552[/img]
点开错误的文件,标注错误的地方是这样的一段代码:
[img]http://files.jb51.net/file_images/article/201706/2017617142610696.png?2017517142619[/img]
import {normalTime} from './timeFormat';
module.exports={
normalTime
};
就是[code]module.exports;[/code]
[b]解决方法[/b]
同过谷歌查找,和论坛各种搜索:
原因如下:[code]The code above is ok. You can mix require and export. You can‘t mix import and module.exports.[/code]
翻译过来就是说,代码没毛病,在webpack打包的时候,可以在js文件中混用require和export。但是不能混用import 以及[code]module.exports [/code]。
因为webpack 2中不允许混用import和[code]module.exports[/code] ,
解决办法就是统一改成ES6的方式编写即可.
[img]http://files.jb51.net/file_images/article/201706/2017617142714346.png?2017517142744[/img]
import {normalTime} from './timeFormat';
export default normalTime;
[b]再次运行:[/b]
[img]http://files.jb51.net/file_images/article/201706/2017617142812646.png?2017517142819[/img]
[b]总结[/b]
以上就是这文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对编程素材网的支持。