在本地html中使用es6的导入导出,在浏览器中打开会报错,Uncaught SyntaxError: Cannot use import statement outside a module,
错误实例:
html文件引用
<script src="./test1.js"></script>
test1.js
import {aaa} from './test2.js'
console.log(aaa);
test2.js
export default {
aaa
}
这里报错的原因是用了 es6 的语法, 浏览器默认将它作为 js 解析会出现问题,需要将它作为模块导入, script 标签默认type="text/javascript",需要改为type="module",更改后的html引用方式:
<script src="···" type="module"></script>