簡體   English   中英

嘗試從json文件加載位置時未顯示React Native圖像

[英]React Native image not showing when try to load location from json file

這是一個json文件,其中存儲每個元素的標題和圖像位置。

 [ {"key": "a", "title": "Bangladesh", "image": "require('./img/bangladesh.jpg')"}, {"key": "b", "title": "Sports", "image": "require('./img/sports.jpg')"}, {"key": "c", "title": "Politics", "image": "require('./img/politics.jpg')"}, {"key": "d", "title": "Entertainment", "image": "require('./img/entertainment.png')"}, {"key": "e", "title": "Economics", "image": "require('./img/economics.jpg')"}, {"key": "f", "title": "Technology", "image": "require('./img/technology.jpg')"}, {"key": "g", "title": "Others", "image": "require('./img/m.jpg')"}, ] 

現在,我想使用平面列表顯示所有圖像和標題。 標題顯示完美,但圖像未顯示。

 <FlatList horizontal= {true} data={newsCategory} renderItem={({item}) => ( <TouchableOpacity style={styles.option} > <Image style={styles.imgButton} source={item.image} /> <Text style={styles.buttonText}>{item.title}</Text> </TouchableOpacity> ) } /> 

我檢查圖像位置是否完全來自json文件,但未顯示圖像。 不明白為什么會這樣。

從require中刪除字符串。

改變這個

[
    {"key": "a", "title": "Bangladesh", "image": "require('./img/bangladesh.jpg')"},
    {"key": "b", "title": "Sports", "image": "require('./img/sports.jpg')"},
    {"key": "c", "title": "Politics", "image": "require('./img/politics.jpg')"},
    {"key": "d", "title": "Entertainment", "image": "require('./img/entertainment.png')"},
    {"key": "e", "title": "Economics", "image": "require('./img/economics.jpg')"},
    {"key": "f", "title": "Technology", "image": "require('./img/technology.jpg')"},
    {"key": "g", "title": "Others", "image": "require('./img/m.jpg')"},
]

對此

[
    {"key": "a", "title": "Bangladesh", "image": require('./img/bangladesh.jpg')},
    {"key": "b", "title": "Sports", "image": require('./img/sports.jpg')},
    {"key": "c", "title": "Politics", "image": require('./img/politics.jpg')},
    {"key": "d", "title": "Entertainment", "image": require('./img/entertainment.png')},
    {"key": "e", "title": "Economics", "image": require('./img/economics.jpg')},
    {"key": "f", "title": "Technology", "image": require('./img/technology.jpg')},
    {"key": "g", "title": "Others", "image": require('./img/m.jpg')},
]

您的圖像源是字符串。 您應該評估它。

<FlatList horizontal= {true}
  data={newsCategory}
    renderItem={({item}) => (
      <TouchableOpacity
        style={styles.option}
      >
      <Image
        style={styles.imgButton}
        source={eval(item.image)}
      />
      <Text style={styles.buttonText}>{item.title}</Text>
    </TouchableOpacity>
     )
   }
/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM