索引文件中导出/导入ES6 [英] ES6 exporting/importing in index file
问题描述
我正在通过webpack / bable在React应用程序中使用ES6。
我正在使用索引文件来收集模块的所有组件并导出它们。不幸的是,它看起来像这样:
从'./Comp1.jsx'导入Comp1_;
从'./Comp2.jsx'导入Comp2_;
从'./Comp3.jsx'导入Comp3_;
export const Comp1 = Comp1_;
export const Comp2 = Comp2_;
export const Comp3 = Comp3_;
所以我可以很好地从其他地方导入它:
$来自'./components'的b $ b
import {Comp1,Comp2,Comp3};
显然这不是一个很好的解决方案,所以我想知道,如果有其他方式。我似乎无法直接导出导入的组件。
最好的问候
您可以轻松地导出默认导入:
从'./导出{默认为Comp1} Comp1.jsx;
export {default as Comp2} from'./Comp2.jsx';
export {default as Comp3} from'./Comp3.jsx';
还有一个 ES8的提案中,您可以使用...导出Comp1; 。 p> I am currently using ES6 in an React app via webpack/bable.
I am using index files to gather all components of a module and export them. Unfortunately, that looks like this: So I can nicely import it from other places like this: 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. Best regards You can easily re-export the default import: There also is a proposal for 这篇关于索引文件中导出/导入ES6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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_;
import { Comp1, Comp2, Comp3 } from './components';
export {default as Comp1} from './Comp1.jsx';
export {default as Comp2} from './Comp2.jsx';
export {default as Comp3} from './Comp3.jsx';
ES7 ES8 that will let you write export Comp1 from '…';
.