ES6 在索引文件中导出/导入 [英] ES6 exporting/importing in index file

查看:31
本文介绍了ES6 在索引文件中导出/导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过 webpack/babel 在 React 应用程序中使用 ES6.我正在使用索引文件来收集模块的所有组件并导出它们.不幸的是,它看起来像这样:

I am currently using ES6 in an React app via webpack/babel. I am using index files to gather all components of a module and export them. Unfortunately, that looks like this:

import Comp1_ from './Comp1.jsx';
import Comp2_ from './Comp2.jsx';
import Comp3_ from './Comp3.jsx';

export const Comp1 = Comp1_;
export const Comp2 = Comp2_;
export const Comp3 = Comp3_;

所以我可以像这样从其他地方很好地导入它:

So I can nicely import it from other places like this:

import { Comp1, Comp2, Comp3 } from './components';

显然这不是一个很好的解决方案,所以我想知道是否还有其他方法.我好像不能直接导出导入的组件.

Obviously that isn't a very nice solution, so I was wondering, if there was any other way. I don't seem to able to export the imported component directly.

推荐答案

您可以轻松地重新导出默认导入:

You can easily re-export the default import:

export {default as Comp1} from './Comp1.jsx';
export {default as Comp2} from './Comp2.jsx';
export {default as Comp3} from './Comp3.jsx';

还有一个 ES7 ES8 的提案,可以让你write export Comp1 from '...';.

这篇关于ES6 在索引文件中导出/导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆