File tree Expand file tree Collapse file tree 1 file changed +44
-1
lines changed
1-js/13-modules/03-modules-dynamic-imports Expand file tree Collapse file tree 1 file changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -45,7 +45,50 @@ import(modulePath)
4545
4646Or, we could use ` let module = await import (modulePath)` if inside an async function.
4747
48- Like this:
48+ For instance, if we have the following ` say .js ` :
49+
50+ ` ` ` js
51+ // 📁 say.js
52+ export function hi () {
53+ alert (` Hello` );
54+ }
55+
56+ export function bye () {
57+ alert (` Bye` );
58+ }
59+ ` ` `
60+
61+ ...Then dynamic import can be like this:
62+
63+ ` ` ` js
64+ let {hi, bye} = await import (' ./say.js' );
65+
66+ hi ();
67+ bye ();
68+
69+ ` ` `
70+
71+ Or, for the default export:
72+
73+ ` ` ` js
74+ // 📁 say.js
75+ export default function () {
76+ alert (" Module loaded (export default)!" );
77+ }
78+ ` ` `
79+
80+
81+ The default export becomes ` default` property in the module object, as explained in the [previous chapter](info:import-export).
82+
83+ So, the dynamic import will be like this:
84+
85+ ` ` ` js
86+ let {default: say} = await import (' ./say.js' ); // map .default to say variable
87+
88+ say ();
89+ ` ` `
90+
91+ Here's the full example:
4992
5093[codetabs src="say" current="index.html"]
5194
You can’t perform that action at this time.
0 commit comments