2016-11-11 55 views
0

不知何故我的图像位置未找到从道具载入。无法从道具载入图像

这工作

let overview_bg = require('../../images/locaties/placeholder.png'); 
<Image source={overview_bg} style={styles.overview_bg}> 

但是,当我从道具获得url它没有:

let overview_bg = require(this.props.image); 
<Image source={overview_bg} style={styles.overview_bg}> 

this.props.image有这给了我错误的网址完全相同:

Requiring unknown module "../../images/locaties/placeholder.png". If you are sure the module is there, try restarting the packager or running "npm install"

我已经检查了多次url,它是完全一样的。我也测试过它在console.log

道具是从另一个容器,从商店得到它。

+0

对不起,我忘了在第一个例子 –

+0

而且添加扩展,笔者从卖场得到的值存储在一个Ajax价值终极版,依然存在。我不确定这是否与它有任何关系,因为它已经存在并且我得到的错误知道值是我需要的确切url。 –

+0

正如David指出的那样,require()仅适用于静态资源。可能你需要传递require('something.jpg')作为props.image,然后直接设置

回答

0

React Native Image documentation

Note that in order for this to work, the image name in require has to be known statically.

// GOOD 
<Image source={require('./my-icon.png')} /> 

// BAD 
var icon = this.props.active ? 'my-icon-active' : 'my-icon-inactive'; 
<Image source={require('./' + icon + '.png')} /> 

// GOOD 
var icon = this.props.active ? require('./my-icon-active.png') : require('./my-icon-inactive.png'); 
<Image source={icon} />