import
基本语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import defaultExport from "module-name";
import * as name from "module-name";
import { export1 } from "module-name";
import { export1 as alias1 } from "module-name";
import { export1 , export2 } from "module-name"; import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, * as name from "module-name"; import defaultExport, { export1 [ , [...] ] } from "module-name";
import "module-name";
(async () => { if (somethingIsTrue) { await import('/modules/my-module.js'); } })();
|
export
基本语法
ESM 模块导出有两种类型:具名导出和默认导出。每个模块可以有多个具名导出,但只能有一个默认导出。两种类型导出的基础语法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
export { myFunction, myVariable };
export let myVariable = ...; export function myFunction() { ... };
export { myFunction as default };
export default function () { ... }
export { default as function1, function2 } from 'bar.js';
|