【发布时间】:2016-11-02 11:54:06
【问题描述】:
我正在使用 react native,我正在为我的按钮创建一个类。该按钮包含一个图像。在这里,我已经在我的视野中减速了我的按钮。
var imageUrl = { //this is a var from a JSON object there could be more than 20 different images
normal: "../assets/images/emoticons/1.png",
selected: "../assets/images/emoticons/1-selected.png"
}
<Button key={index}
selected={true}
onPress={onSelect}
imageUrl={imageUrl} />
在“按钮”类中,我创建了一个 _renderImage 函数,用于设置按钮中的图像。
_renderImage(imageUrl){
let image = (selected === true) ? require(imageUrl.normal) : require(imageUrl.selected); //get the right image url
console.log('../assets/images/emoticons/1.png'); //logs '../assets/images/emoticons/1.png'
console.log(typeof('../assets/images/emoticons/1.png')); //logs string
console.log(image); //logs '../assets/images/emoticons/1.png'
console.log(typeof(image)); //logs string
return (
<Image source={require(image)} />
);
}
当我手动设置<Image source= require('../assets/images/emoticons/1.png)} /> 时,它会渲染图像。所以图像存在于这个位置。
类似的问题并不能解决我的问题: Trouble requiring image module in React Native
【问题讨论】:
标签: javascript reactjs react-native var