我正在尝试执行以下操作:
var itemImage = '../img/image1.jpg'
return(
<Image
source={require(itemImage)}
/>
但是一直报错:
Requiring unknown module '../img/image1.jpg'.
但是,当我直接在 require() 中输入:'../img/image1.jpg' 时,它就起作用了。
可能是什么问题?任何指导或见解将不胜感激。
编辑***
我的构造函数中有以下作为我的数据源:
this.state = {dataSource: ds.cloneWithRows([{name: '../img/item1.jpg'}, {name: '../img/item2.jpg'}])}
然后在我的 renderRow() 中,我试图实现如下内容,以便它根据数据源进行更新:
renderRow(rowData){
var itemImage = require(rowData.name)
return(
<Image
source={itemImage}
/>
但我仍然得到同样的错误:
Requiring unknown module '../img/image1.jpg'.
最佳答案
在图像源中使用单个大括号试试这个。 并使用 require() 方法将图像路径存储在图像变量中。
render(){
var image=require('../img/image1.jpg');
return(
<View >
<Image source={image}/>
</View>
);
}
关于javascript - 为什么不能在 <Image source={}/> 中使用字符串变量? ( native react ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38366191/