【发布时间】:2017-04-23 00:53:45
【问题描述】:
我使用react 来构建我的组件库。我需要有一个index.js 才能在一个地方导入所有组件。
类似这样的:
MyComponents/
Button.js
Label.js
index.js
在index.js里面我接下来尝试做:
// this export nothing
export {default} from './Button';
// this tells me about syntax error
export default from './Button';
我发现只有这个解决方案有效
import Button from './Button';
export default Button;
但我发现一些 React 组件库使用了我上面提到的语法 (export default from './Button') - 它以某种方式工作。看起来他们使用了一些 babel-plugin-transform-* 来执行此操作。
请找我了解如何将我的两行导入导出转换为export ... from ... 的一行。谢谢。
附:最后我需要这样做:import Button from './MyComponents';
【问题讨论】:
-
请尝试
export {default as Button} from './Button'。 -
遗憾的是它不起作用。它可以编译,但不允许从模块中导入 Button。
-
我复制了你的例子,
export default from './Button'为我工作。编译并正确显示。我使用以下 Babel 预设:"es2015", "stage-0", "react"。你能告诉我你的 Babel/Webpack 配置吗? See a screenshot here -
嗨。我不使用
stage-0或stage-1,因为这是一个实验性功能。这可能是一个原因。 -
哦,是的。最后我在这里找到了这个提案的描述:github.com/leebyron/ecmascript-export-default-from
标签: javascript node.js reactjs npm babeljs