在用 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"属性。