2016-02-28 44 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