javascript - "export default from"不适用于 Babel React

标签 javascript node.js reactjs npm babeljs

我使用 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-* 来做到这一点。

请找我了解如何将我的两行 import export 转换为一行 export ... from ...。谢谢。

附言最后我需要这样做:import Button from './MyComponents';

最佳答案

使用以下语法:

File: layouts/index.js

export {default as Flex} from './flex'
export {default as Grid} from './grid'
export {default as Dock} from './dock'

然后就可以使用了

import { Dock, Grid } from 'layouts'

import layouts from 'layouts'

<layouts.Flex >...</layouts.Flex>

为了导出由上述方法创建的嵌套索引,您可以使用 export * 语法:

File: icons/index.js

export * as action from './action';
export * as alert from './alert';
export * as av from './av';

用法:

import * as icons from 'material/icons'

<icons.action.Close />

关于javascript - "export default from"不适用于 Babel React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41028317/

相关文章:

javascript - 使用 Socket.IO 获取 404

javascript - 为什么要删除我的类(class)名称?

javascript - 将数据从 Props 传递到 vue.js 中的数据

javascript - 静态网站中的表格分页问题

node.js - 使用模型作为条件在 Waterline 中查询

json - 语法错误: Failed to parse JSON body: Unexpected token o

javascript - 类型错误 : Cannot read property 'exists' of undefined

css - 如何使用自定义样式在 Reactjs 中使用 html2canvas 截取 DOM 的屏幕截图

javascript - 从左到右对 SVG 线进行动画处理 Snap.svg

javascript - 使用 gulp 时显示 javascript 错误