作为个人项目的一部分,我必须创建一个图片库。对于在图库中单击的每个图像(微型),我希望所选图像以更大的尺寸显示在页面上。我一直试图通过将所需图像的地址作为 Prop 传递给我的 ImageOnView 组件来实现此目的,如下所示:
<ImageOnView imgSrc = {this.state.imageLarge}/>
但是,在我的 ImageOnView 组件中,我无法使用 require() 方法显示图像。现在看起来像这样:
class ImageOnView extends Component {
render() {
//This alert displays the image source as expected
alert(this.props.imgSrc);
return (
<img id = "image-large" src = {require(this.props.imgSrc)}/>
);
}
}
我的网页上出现以下错误:“错误:找不到模块“。””
我该如何解决这个问题?提前致谢。
最佳答案
尝试一下
src={require('' + this.props.imgSrc)}
关于javascript - 无法使用在 React 中接收 prop 的 require() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51904712/