我正在使用Webpack进行开发(热重新加载),我正在使用import Sample from './components/Sample/Sample'
导入React组件,但是,我想简单地使用import {Sample, SampleTwo} from './components'
导入。
然而,前者的作品会引发错误。
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 :(得分:1)
建议Davin,您只需导出其属性引用您的组件的对象:
import Sample from './components/Sample/Sample';
export { Sample: Sample }
甚至更好:
import Sample from './components/Sample/Sample';
export { Sample }
然后你可以这样导入:
import { Sample } from './components'
答案 1 :(得分: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};