react-native - 图像需要来自JSON的本地路径 [英] react-native - Image require local path from JSON
问题描述
Hello comunity :)我正在使用react-native的testapp,并尝试从本地存储位置获取图像。实际我在做什么:我给一个图像directlink源到var并在我的渲染函数中调用这个方法。
Hello comunity :) I am working on a testapp in react-native and try to get Images from a local storage place. Actual what i am doing: I give an image directlink source to a var and call this method at my render function.
*react: ^0.14.8 ||*
*react-native: ^0.23.1||*
*npm: 2.15.1 ||*
*node: v4.4.4*
工作:
Working:
ImageCall-File(Imagefile.js)
var samplepicture;
setPicture(){
samplepicture= require('./images/picture.png')
}
render() {
this.setPicture;
return(
<Image source=samplepicture/>
);
}
上面的代码片段在一个以后命名的创建类中工作(Imagefile.js)
上面的方法效果很好。
现在我在哪里我想要到>我想将图像文件保存在一个单独的js文件中,其格式类似于JSON对象。
Now my where i want to get to > I want to save the image file in a seperated js file wich is styled like a JSON object.
不工作:
Not working:
JS_JSON_File(pictureRef.js)
module.exports = {
"pictureRef":
{
"picture": './images/picture.png'
}}
ImageCall_File(Imagefile.js)
var pictureRef = require('./pictureRef.js');
var samplepicture;
setPicture(){
samplepicture= require(pictureRef.pictureRef.picture);
}
render() {
this.setPicture;
return(
<Image source=samplepicture/>
);
}
调试samplepicture:
samplepicture = ./images/picture.png
Debug the samplepicture: samplepicture = ./images/picture.png
不确定它是否相关,但因为它是一个字符串,所以没有格式化''。另外请记住,我需要通过setPicture()函数为我的代码设置图像源。
*我调试了两个版本的代码并且有相同的输出:
*I debugged both versions of code and had the same output:
- 版本1:samplepicture = ./ images/picture.png
- 版本2:samplepicture = ./ images/picture.png
*
请记住,这是我的第一个问题,我真的希望将所有内容格式化。此外,我搜索了许多相关的问题,并没有找到任何解决方案
感谢您的帮助。
推荐答案
我认为如果使用require()来添加图像,则应该给出静态路径,而不是变量。因为他们需要知道要捆绑的特定图像源。
使用json就像一个不确定的来源所以它不起作用
现在有一个pr,
i think if you use require() to add your image,you should give a static path,not a variable. because they need know the specific image source to bundle. use json is just like a uncertain source so it doesnt work now there is a pr to this,
这篇关于react-native - 图像需要来自JSON的本地路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!