【发布时间】:2018-10-20 11:02:07
【问题描述】:
我上传的代码较少,因为我认为它已经足够了
询问是否需要更多来查找错误
async componentDidMount(){
var image = this.props.data.img
console.log(this.props.data.img == './../images/thumbnails/g.png') // true
console.log(this.props.data.img === './../images/thumbnails/g.png') // true
console.log(typeof(this.props.data.img))
await import(image)
.then((res)=>{
console.log(res)
this.setState({img:res})
})
.catch((e)=>{
console.log(this.props.data.img)
console.log(e)
})
}
我正在尝试导入图像。
当我使用import('./../images/thumbnails/g.png) 时,它会成功运行,但是当我传递与道具相同的值并尝试导入它时,它会出现错误(未找到模块)。
我记录了这些值,所有的结果都是真的,意味着字符串和道具字符串是相同的,那么为什么我会得到 2 种不同的行为?
【问题讨论】:
-
您无法通过将字符串作为变量传递给
import来导入文件。检查stackoverflow.com/a/47956054/7232300 -
基本上,当您的模块捆绑器正在捆绑您的应用程序时,
this.props.data.img的值不可用,因为该值仅在运行时可用(即捆绑后)。
标签: reactjs react-props react-image