您的位置:首页 > 其它

ES6学习——模块化:Module Loader API

2016-02-07 19:06 549 查看
上篇讲的模块化API总体来讲是个静态导入的过程,就是不能根据程序逻辑动态的导入一些其他模块。在ES6规范的草案阶段有个动态导入的API,但是在ES6正式规范出来的时候被去掉了,大家可以参考https://github.com/ModuleLoader/es6-module-loader,个人觉得这个API还是很有用的,所以这里介绍一下。

其实这个API很简单,基于Promise模式:

//mymodule.js
export class q {
constructor() {
console.log('this is an es6 class!');
}
}


<script>
System.import('mymodule').then(function(m) {
new m.q();
});
</script>


还可以使用type为module的script标签实现动态导入:

<script type="module">
// loads the 'q' export from 'mymodule.js' in the same path as the page
import { q } from 'mymodule';

new q(); // -> 'this is an es6 class!'
</script>


想深入了解的可以自己去那个github仔细学习学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: