Promise.js
export default class Promise{}
index.js
import Promise from './Promise.js' // Uncaught SyntaxError: Unexpected identifier
index.html
<script src="./index.js"></script>
查找到解决方案为:
在index.html文件中,script标签中添加 type="module" 属性。
<script type="module" src="./index.js"></script>
解析:
inport和export是ES6新增的语法糖。浏览器需要使用<script>标签加载js文件;但是要加载ES6的语法规则,就需要加入 type="module" 属性。阮一峰老师的ES6标准入门–Module的加载实现有详细介绍。
博客介绍了在HTML中加载ES6 Module的解决方案,即在index.html文件的script标签中添加特定属性。指出import和export是ES6新增语法糖,浏览器加载ES6语法规则需加入该属性,还提及阮一峰老师的相关书籍有详细介绍。

3450

被折叠的 条评论
为什么被折叠?



