In the last chapter, the modular API is a static import process, that is, it is not possible to import some other modules dynamically according to the program logic. There is a dynamically imported API in the draft phase of the ES6 specification, but it was removed when the ES6 formal specification came out, you can refer to Https://github.com/ModuleLoader/es6-module-loader, Personally think this API is still very useful, so here is a brief introduction.
In fact, this API is very simple, based on promise mode:
Mymodule.js
Export Class Q {
constructor () {
Console.log (' This is a es6 class! ');
}
<script>
system.import (' MyModule '). Then (function (m) {
new m.q ();
});
</script>
You can also implement dynamic import using the script label for module with type:
<script type= "module" >
//Loads the ' Q ' Export from ' mymodule.js ' same path as the page
import {q} From ' MyModule ';
New Q (); -> ' This is a es6 class! '
</script>
If you want to know more, you can go to the GitHub and study carefully.