【问题标题】:When we `import { foo }`, why can't we `export foo` but have to `export { foo }`?当我们`import { foo }`时,为什么我们不能`export foo`而必须`export { foo }`?
【发布时间】:2017-12-09 07:45:36
【问题描述】:

我想知道为什么我们之后

import { foo } from "some/path";

我们没有

export foo;

像往常一样,但必须

export { foo };

为了能够导出?

原因是我们通常导出一个“命名”的导出方式

export const bar = 123;

const wah = 234;
export wah;

他们在没有{ } 的情况下导出。

【问题讨论】:

  • 导入和导出 foo 是否发生在同一个文件中?
  • 你确定它有效吗?根据MDN,export wah; 无效。

标签: reactjs webpack ecmascript-6


【解决方案1】:

没有。 export foo; 或 export wah; 不是通常。他们只是不工作。

您通常使用任一方法创建命名导出

export const wah = 234;

或

const wah = 234;
export { wah }

或

const bar = 234;
export { bar as wah }

顺便说一句,你也可以直接做

export { foo } from "some/path";

【讨论】:

  • 也不是export { foo } 只是export {foo: foo} 的简写
  • @Bulkan 不,这是export { foo as foo } 的简写(参见我的bar 示例)
猜你喜欢
  • 1970-01-01
  • 2011-09-10
  • 2015-10-17
  • 2013-03-21
  • 2012-01-25
  • 2020-10-03
  • 2010-12-12
  • 2015-11-21
  • 2017-05-15
相关资源
最近更新 更多