【问题标题】:Re-export a default export重新导出默认导出
【发布时间】:2016-01-03 11:55:58
【问题描述】:

从另一个文件重新导出默认导出的最佳方法是什么?

我想做这样的事情:

export Button from './Button/Button';

显然这不是有效的 EcmaScript 语法(但它曾经在旧 Babel 版本中工作)。

结果我想import {Button} from 'components';

有没有更好的方法,也许是单线?优先考虑 ES6 语法。

谢谢!

【问题讨论】:

  • 为什么?为什么不只是import {Button} from 'components/Button' 之类的?

标签: javascript babeljs ecmascript-6


【解决方案1】:

您可以使用export { x as y } 语法:

export { default as Button } from './Button/Button';

我想做这样的事情:

export Button from './Button/Button';

此语法有一个阶段 1 提案:https://github.com/leebyron/ecmascript-more-export-from

【讨论】:

  • 不幸的是export { default as y } from './m' 对我不起作用。我在 m.js 中有 export default function () { /* ... */ }。使用 i.js 中的 export { default as ... 语句,我在另一个文件中尝试 import { y } from './i',但 y 未定义。我在节点 4.2 中使用 babel 6 require-hook 和 polyfill。
  • 没关系,找到原因了:add-module-exports pluginexport { default as x } from '...' 不起作用的原因。
猜你喜欢
  • 2017-07-09
  • 1970-01-01
  • 2021-10-12
  • 2019-12-06
  • 2018-07-21
  • 1970-01-01
  • 2017-04-29
  • 2016-01-23
  • 1970-01-01
相关资源
最近更新 更多