export 与import是es6中新增模块功能重要的, 并且目前也是很常用的两个用法。
现在来看看export
和export default
两者之间的区别。话说个人认为这两种用法和使用场景还真的不太好区分,似乎在各个场景里两者都是可以派的上用场?
export
export 适用于函数(function)/类(class)/常量(const)等
我们在导入引用时, 需要加上大括号{}
示例如下:
// a.example.ts
export class A {
// ...
}
// b.example.ts
import { A } from './a.example';
export default
export default适用于某个模块, 一个文件中只能导出一次。(export则不限)
另外我们在引用时, 不需要加大括号{}
示例如下:
// a.example.ts
export default class A {
// ...
}
// b.example.ts
import A from './a.example';
而其实两者也是可以组合来用的,比如
export defaultSomeModlue, { A } from 'xxx';
参考链接: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export