在用 import命令导入模块时 控制台显示错误:Uncaught SyntaxError: Cannot use import statement outside a module

解决方法:在<script> 标签内添加  type="module" 属性。

<script type="module" src="./src/index_import.js"></script>

错误原因:

从报错信息中获知:无法在模块外部使用 import语句。这是因为 模块化(Module)的加载是用 ES6语法实现的,在HTML网页中,浏览器通过 script标签加载 JS脚本,但浏览器默认的脚本语言是 JavaScript (故 type="application/javascript" 可以省略),所以浏览器在加载 html文件时,需要在 script标签内加入 type="module"属性。

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/donghuang/p/12496352.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!