javascript - 如何访问本地 JSON 数组中的图像
问题描述
Hijama = [
{
name: 'Body Back',
pic: '../Images/BackSide.png',
},
{
name: 'Body Front',
pic: '../Images/images.jpg',
},];
//这里我想渲染我的照片
render_swiper_data = () => {
return this.Hijama.map((item, index) => {
console.log('check', item);
return (
<View key={index}>
<Text>{item.name}</Text>
<Image
source={require(item.pic)}
style={{
height: '100%',
width: '100%',
}}
/>
</View>
);
})};
//现在我的问题是我如何访问这些图像,我尝试了 Require 和 uri,,,
解决方案
你不能像那样使用 require 。react-native 无法读取。所以你必须像这样存储所有本地图像位置:
Hijama = [
{
name: 'Body Back',
pic: require('../Images/images.jpg'),
},
{
name: 'Body Front',
pic: require('../Images/images2.jpg')
},];
然后在您的代码中使用它:
render_swiper_data = () => {
return this.Hijama.map((item, index) => {
console.log('check', item);
return (
<View key={index}>
<Text>{item.name}</Text>
<Image
source={item.pic}
style={{
height: '100%',
width: '100%',
}}
/>
</View>
);
})};
推荐阅读
- json - 从 API 调用返回的记录(对象)太大而无法写入单个单元格
- python - 如何在python中将对象解析为字符串?
- java - onCreate 中的 MapView NullPointerException 仅使用最新的 GooglePlayServices
- ruby-on-rails - Button_to 指向的补丁/更新方法不起作用
- python - 不执行for循环时python中的for-else
- java - 不首先在 Android 的 NextPageToken Youtube API3 中加载最新视频
- .htaccess - HTACCESS 重写的 URL 正在重定向查询参数
- python - 在热图轴上进行注释
- java - 程序跳过'if'语句并默认为'else'
- sass - Sass vs Scss - 我应该如何搜索 SASS?