调用 map 函数后无法加载图像:
this.rosImgs.push({img: '../imgs/ros_eat.png', id: this.imgId});
getRosette(){
return this.rosImgs.map((obj) =>
<TouchableHighlight key={obj.id} onPress={() => this.imgClick()}>
<Image
source={require(obj.img)}
/>
</TouchableHighlight>
);
}
出现以下错误:
Unknown named module:'../imgs/ros_eat.png'
但是当我这样做时 source={require('../imgs/ros_eat.png')}
它有效。
请帮忙
最佳答案
在React Native中,图像无法动态加载并在之前解析。所以我建议更改如下代码。
this.rosImgs.push({img: require('../imgs/ros_eat.png'), id: this.imgId});
getRosette(){
return this.rosImgs.map((obj) =>
<TouchableHighlight key={obj.id} onPress={() => this.imgClick()}>
<Image
source={obj.img}
/>
</TouchableHighlight>
);
}
关于javascript - 在返回的map()对象中设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41305591/