2016-02-28 81 views
0

我正在开发与Webpack(热重新加载),我正在导入React组件import Sample from './components/Sample/Sample',但是,我想简单地能够导入import {Sample, SampleTwo} from './components'即时ES6导出似乎不工作

前者的作品,但后者会引发错误。

components/ 
     index.jsx 
     Sample/ 
      Sample.jsx 
     SampleTwo/ 
      SampleTwo.jsx 

内index.jsx的,我已经试过:

export {default as Sample} from './component/Sample/Sample'它的工作原理,但我得到的WebPack警告说这是在只读模式。然后,我试过如下:

import Sample from './components/Sample/Sample'; 

    export default { 
     Sample: Sample 
    } 
+0

“,然而,后者引发错误。 “ ---那个错误是......? – zerkms

+0

你需要一个'default'吗?为什么不在默认情况下'输出',那么导入时全部都应该工作。 –

+0

这个讨论可能有所帮助https://github.com/gaearon/react-hot-loader/issues/158 – kaushik94

回答

1

由于Davin建议,你可以只是出口其属性引用您的组件对象:

import Sample from './components/Sample/Sample'; 

export { Sample: Sample } 

甚至更​​好:

import Sample from './components/Sample/Sample'; 

export { Sample } 

然后你可以这样导入:

import { Sample } from './components' 
+0

是的,我试过这个,但是,它似乎并没有在应用程序内工作:( – Detuned

+0

什么错误你看到了吗? –

+0

它只是说模块找不到了,我回到了典型的方式,所以我尝试了另一种语法,但是,我得到了Hot Reloading警告,正如我在 – Detuned

0

部件/样品/ Sample.jsx

[...] 

class Sample extends React.Component 
{ 
    [...] 
} 

export default Sample; 

组件/ SampleTwo/SampleTwo.jws

[...] 

class SampleTwo export React.Component 
{ 
    [...] 
} 

export default SampleTwo; 

组件/ index.jsx

import Sample from './Sample/Sample' 
import SampleTwo from './SampleTwo/SampleTwo' 

export {Sample, SampleTwo}; 
+0

我试过这个,但是它没有工作:( – Detuned

+0

你看到了什么错误,并且你能显示你的''webpack.config.js''吗? – vbarbarosh