【发布时间】:2018-08-09 00:07:06
【问题描述】:
我在访问 json 文件中的本地图像 url 时遇到问题。以下是我尝试过的所有方法,但它们都不起作用。
// json
{
"image": "./images/example.jpg",
}
// React Native
import data from './data.json';
const URL = JSON.stringify(data.image)
<Image source = {{uri: URL } style = {{ width: 98, height: 22 }} /> // Image not showing
const URL = data.image
<Image source = {{uri: URL } style = {{ width: 98, height: 22 }} /> // Image not showing
上述方法中未显示图像。
我也尝试过如下要求:
// json
{
"image": "example.jpg",
}
const item = data.image
<Image source = {require( './images/' + item)} style = {{ width: 98, height: 22 }} />
const item = JSON.stringify(data.image)
<Image source = {require( './images/' + item)} style = {{ width: 98, height: 22 }} />
上面显示了需要只接受字符串文字参数的错误,这对我来说很奇怪,因为下面的代码确实有效。
const item = 'example.jpg'
<Image source = {require( './images/' + item)} style = {{ width: 98, height: 22 }} />
我在互联网上搜索了几个小时,但找不到解决方案。有人可以向我展示一个访问本地 json 数据的正确示例吗?非常感谢。
【问题讨论】:
标签: json react-native