1.基本用法
import defaultExport from "module-name";
import * as name from "module-name";
import { export } from "module-name";
import { export as alias } from "module-name";
import { export1 , export2 } from "module-name";
import { foo , bar } from "module-name/path/to/specific/un-exported/file";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
// Exporting individual features
export let name1, name2, …, nameN; // also var, const
export let name1 = …, name2 = …, …, nameN; // also var, const
export function functionName(){...}
export class ClassName {...}
// Export list
export { name1, name2, …, nameN };
// Renaming exports
export { variable1 as name1, variable2 as name2, …, nameN };
// Exporting destructured assignments with renaming
export const { name1, name2: bar } = o;
// Default exports
export default expression;
export default function (…) { … } // also class, function*
export default function name1(…) { … } // also class, function*
export { name1 as default, … };
// Aggregating modules
export * from …; // does not set the default export
export * as name1 from …; // Draft ECMAScript® 2O21
export { name1, name2, …, nameN } from …;
export { import1 as name1, import2 as name2, …, nameN } from …;
export { default, … } from …;
export导出模块
2.1 导入整个模块的内容
这将myModule插入当前作用域,其中包含来自位于/modules/my-module.js文件中导出的所有接口。
const myExport = 1
const otherExport = 2
const doAllTheAmazingThings = () => {}
export { myExport,otherExport,doAllTheAmazingThings}
上述写法的别名写法:
const myExport = 1
const otherExport = 2
const doAllTheAmazingThings = () => {}
export { myExport as alias1,otherExport as alias2,doAllTheAmazingThings}
export const myExport = 1
export const otherExport = 2
export const doAllTheAmazingThings = () => {}
import * as myModule from '/modules/my-module.js';
在这里,访问导出接口意味着使用模块名称(在本例为“myModule”)作为命名空间。例如,如果上面导入的模块包含一个接口doAllTheAmazingThings(),你可以这样调用:
myModule.doAllTheAmazingThings();
2.2 导入部分个接口
写法一和写法二等价
写法一:
export const myExport = 1
export const otherExport = 2
写法二:
const myExport = 1
const otherExport = 2
export {myExport,otherExport}
别名写法:
const myExport = 1
const otherExport = 2
export {myExport as alias1,otherExport}
import {myExport} from '/modules/my-module.js';
2.3 仅为副作用而导入一个模块
整个模块仅为副作用(中性词,无贬义含义)而导入,而不导入模块中的任何内容(接口)。 这将运行模块中的全局代码, 但实际上不导入任何值。
import '/modules/my-module.js';
3 export default导出模块
引入模块可能有一个defaultexport(无论它是对象,函数,类等)可用。然后可以使用import语句来导入这样的默认接口。
cosnt myFunction = () => {}
// export feature declared earlier as default
export { myFunction as default };
// export individual features as default
export default function () { ... }
export default class { .. }
// each export overwrites the previous one
注意:每一个export都会覆盖上面定义的default.
例1: 最简单的用法是直接导入默认值:
// module "my-module.js"
export default function cube(x) {
return x * x * x;
}
import cube from './my-module.js';
console.log(cube(3)); // 27
上述的变量cube可以为其他变量。
例2:defalut的使用
export { default as function1,function2 } from 'bar.js';
import { default as function1,function2 } from 'bar.js';
export { function1 as default, function2 };