我们一般知道浏览器不支持es6可以依赖babel开进行转码,使es6代码可以正常运转在不支持的浏览器上,类似的还有两个转码器。
ES6 module transpiler
首先,安装这个转玛器。
$ npm install -g es6-module-transpiler
然后,使用compile-modules convert命令,将 ES6 模块文件转码。
$ compile-modules convert file1.js file2.js
-o参数可以指定转码后的文件名。
$ compile-modules convert -o out.js file1.js
SystemJS
使用时,先在网页内载入system.js文件。
<script src="system.js"></script>
然后,使用System.import方法加载模块文件。
<script>
System.import('app/es6-file').then(function(m) {
console.log(new m.q().es6); // hello
});
</script>