4
我有一个与Webpack捆绑在一起的React项目。动态导入未绑定文件
我有一个组件,我希望它动态地呈现组件。就我而言,组件的路径来自道具。
此外,这些组件未捆绑在我的项目.js文件中;它们是外部React组件/库文件。
我已经试过了动态ES6进口:
componentWillReceiveProps(nextProps){
if(nextProps.pagesData && this.props.pagesData !== nextProps.pagesData && nextProps.pagesData.get('cards').count() > 0){
// Getting the first card from the Immutable object
let card = nextProps.pagesData.getIn(['cards', 0]);
// Getting the cardType which can be: '/path/index.js'
let cardType = card.get('card_type');
// ES6 Dynamic import
import(cardType)
.then(module => {
this.setState({ asyncCard: module.default });
})
}
}
这不起作用,因为进口需要一个静态路由。
然后我试着要求:
let dynamicComponent = require(cardType);
不工作,因为(我认为)的WebPack试图找到它进入主束。
这甚至有可能吗?
更新:它看起来像这样可以工作(cardType是 'index.js' - 一个阵营成分):
import(`/home/user/_apps/module/react-module/lib/${cardType}`)
的WebPack创建一个不同的束(块)包括index.js的代码和所有它的依赖。
但这并不能真正解决我原来的问题。
编辑2:从上面的导入实际上忽略了最后一个var,并且Webpack使得/ lib中的每个文件都成为块。