环顾下一个我找不到答案:我如何动态地包含一个文件,基于每个说道具的改变:这里有一些sudo代码,试图做!动态地包含文件(组件)并动态注入这些组件
class Test extends React.Component {
constructor(props){
super(props)
this.state = { componentIncluded: false }
includeFile() {
require(this.props.componetFileDir) // e.g. ./file/dir/comp.js
this.setState({ componentIncluded: true });
}
render() {
return(
<div className="card">
<button onClick={this.includeFile}> Load File </button>
{ this.state.componentIncluded &&
<this.state.customComponent />
}
</div>
)
}
}
所以this.props.componetFileDir
访问的文件目录,但我需要动态包括它,并不能真正确实需要()作为它似乎动作onClick
被调用之前运行。任何帮助都会很棒。
无论如何,'Button'组件将被导入,你为什么不想要做简单的导入,然后管理导入组件的行为? –
我的答案能解决您的问题吗? –
因为'webpack'的稳定版本不支持'System.import',所以我使用'require.resolve'工作,并且不想在一个函数中包含'system.js'。 – Kivylius