export:命名导出
export default:默认导出
你能够在每一个模块中定义多个命名导出,但是只允许有一个默认导出。
// 导出单个特性
export let name1, name2, …, nameN; // also var, const
export let name1 = …, name2 = …, …, nameN; // also var, const
export function FunctionName(){...}
export class ClassName {...}
// 导出列表
export { name1, name2, …, nameN };
// 重命名导出
export { variable1 as name1, variable2 as name2, …, nameN };
// 解构导出并重命名
export const { name1, name2: bar } = o;
// 默认导出
export default expression;
export default function (…) { … } // also class, function*
export default function name1(…) { … } // also class, function*
export { name1 as default, … };
// 导出模块合集
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 命名导出示例:
// module "my-module.js"
function cube(x) {
return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
var graph = {
options: {
color: "white",
thickness: "2px",
},
draw: function () {
console.log("From graph draw function");
},
};
export { cube, foo, graph };
import { cube, foo, graph } from "my-module.js";
graph.options = {
color: "blue",
thickness: "3px",
};
graph.draw();
console.log(cube(3)); // 27
console.log(foo); // 4.555806215962888
export default 默认导出示例:
// module "my-module.js"
export default function cube(x) {
return x * x * x;
}
import cube from "./my-module.js";
console.log(cube(3)); // 27