使用命名导出
export { a, b, c};
import { a, b, c} from 'xxx.js';
使用默认导出
// module "my-module.js"
export default function cube(x) {
return x * x * x;
}
import cube from 'my-module.js';
console.log(cube(3)); // 27
注意,不能使用 var、let 或 const 用于导出默认值 export default。例:
export default const/let/var variable = xxx 均不可使用此类方式;
可使用方式:const a = 1; export default a;
模块重定向
如果我们想要在当前模块中,导出指定导入模块的默认导出(等于是创建了一个“重定向”):
// module "redirect-module.js"
export {default} from './other-module';
export * from './other-module';