【问题标题】:How to re-export another module's default export? [duplicate]如何重新导出另一个模块的默认导出? [复制]
【发布时间】:2017-04-29 14:44:42
【问题描述】:

我想像下面这样导出一些模块但总是失败..

foo.js

const foo = {
  a: 'b'
};
export default foo;

index.js

export foo from './foo';  // encounter error here
export * from './foo';    // it works..

我不知道为什么我不能使用第一种方法从foo.js 导出模块,在我看来,我可以导出函数、类、变量等任何东西。

【问题讨论】:

  • @vsync 非常抱歉,我会尽快更改标题,谢谢您的回复。
  • @vsync 再次感谢,这是我的错误 :(

标签: javascript ecmascript-6


【解决方案1】:

要将一个模块的默认导出导出为另一个模块的命名导出,您必须这样做:

// index.js
export { default as foo } from './foo';

您现在可以将 foo 作为命名导出导入到其他地方:

// another.js
import { foo } from './index'

【讨论】:

  • 感谢您的回复。
  • 虽然我理解这个例子,但我想知道这里的花括号是否有任何原因。为什么以下不可能(不使用 Babel)? export default from './foo';(在 index.js 中)然后将其导入为 import myDefault from './index.js' (在 another.js 中)
猜你喜欢
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 2017-05-11
  • 2019-11-22
  • 2016-04-17
  • 2018-05-16
相关资源
最近更新 更多