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的加载实现有详细介绍。