react.js - 用字符串变量取本地图片的问题,哪位达人说一下。
本文介绍了react.js - 用字符串变量取本地图片的问题,哪位达人说一下。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
这行代码是可以顺利工作的:
<Image style={styles.thumbnail}
source={ require( './image/lg.jpg' ) }
这段代码:
var tempString = './image/lg.jpg';
......
<Image style={styles.thumbnail}
source={ require( tempString ) }
报错说:Requiring unknown module "./image/lg.jpg". If you are sure the module is there, try restarting the packager.
这是怎么回事儿?直接写死文件名可以运行,展示图片。用个字符串变量存储文件名,就不行了......
解决方案
http://facebook.github.io/react-native/docs/images.html#content
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} />
这篇关于react.js - 用字符串变量取本地图片的问题,哪位达人说一下。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文