繁体   English   中英

在ListView中添加存储在JSON数组中的图像 - ReactNative

[英]Adding Images stored in JSON Array in a ListView - ReactNative

我在ReactNative ListView中显示存储在JSON数组中的图像时遇到问题。 让我们说这是我的数据:

var data = [
  {
    id: 0,
    photo: '../images/test.png',
  },
  {
    id: 1,
    photo: '../images/test.png',
  },
  {
    id: 2,
    photo: '../images/test.png',
  },
]

然后,在我的课堂上,首先我需要数据(假设它正确导出):

var data = require("../data").data

然后,对于数据源中的每个元素,我遍历JSON数组,并尝试显示Image:

<Image source={{uri:item.photo}} style={styles.photo}></Image>

但是,屏幕上不显示任何内容。 我知道数据正在被正确拉出,因为其他文本字段(上面未显示)显示正确,例如:

<Text style = {styles.name}>{item.id}</Text>

这显示正常。 此外,如果我尝试直接在源中要求图像,即

<Image source={require('../images/test.png')} style={styles.photo}></Image>

它工作正常。 有人知道为什么Image Source作为变量引用时没有拉动?

你可以这样做:

 var data = [ { id: 0, photo: require('../images/test.png'), }, { id: 1, photo: require('../images/test.png'), }, { id: 2, photo: require('../images/test.png'), }, ] 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM