Javascript 在返回的map()对象中设置图像

Javascript 在返回的map()对象中设置图像,javascript,image,react-native,map-function,Javascript,Image,React Native,Map Function,调用映射函数后无法加载图像: 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

调用映射函数后无法加载图像:

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>
    );
  }
this.rosImgs.push({img:'../imgs/ros_eat.png',id:this.imgId});
getRosette(){
返回此.rosImgs.map((obj)=>
this.imgClick()}>
);
}
出现以下错误:

未知命名模块:“../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>
    );
  }
this.rosImgs.push({img:require('../imgs/ros_eat.png'),id:this.imgId});
getRosette(){
返回此.rosImgs.map((obj)=>
this.imgClick()}>
);
}

@kapuchaster是的,我有类似的要求。我花了好几个小时才弄明白。很高兴它帮助了你!