React native 为什么美元不起作用?
如果我只是设置React native 为什么美元不起作用?,react-native,React Native,如果我只是设置require('../img/dest.png')图像可以工作,但是当我尝试使用like时 它将显示错误 calls to require expect exactly 1 string literal argument but this was found: require(""+item.img+"").(null) 有人知道我的美元变量发生了什么吗 提前谢谢 render() { // Taken from https://flatuicolors.com/ c
require('../img/dest.png')
图像可以工作,但是当我尝试使用like时
它将显示错误
calls to require expect exactly 1 string literal argument but this was found: require(""+item.img+"").(null)
有人知道我的美元变量发生了什么吗
提前谢谢
render() {
// Taken from https://flatuicolors.com/
const items = [
{ name: 'Name', code: '#1abc9c', url: 'yahoo.com.tw', img: '../img/dest.png' }
];
return (
<GridView
itemDimension={130}
items={items}
style={styles.gridView}
renderItem={item => {
console.log(`'${item.img}'`);
return (
<TouchableOpacity onPress={() => this.showLinkAlert(item.name, item.url)}>
<ImageBackground source={require(`'${item.img}'`)} style={[styles.itemContainer, { backgroundColor: '#bdc3c7' }]}>
<Text style={styles.itemName}>{item.name}</Text>
</ImageBackground>
</TouchableOpacity>
);
}}
/>
);
}
}
render(){
//取自https://flatuicolors.com/
常数项=[
{name:'name',code:'#1abc9c',url:'yahoo.com.tw',img:'../img/dest.png'}
];
返回(
{
log(`${item.img}`);
返回(
this.showLinkAlert(item.name,item.url)}>
{item.name}
);
}}
/>
);
}
}
字符串替换发生在运行时,而require
发生在构建时。因此,当require运行时,它不会替换任何内容,它只是尝试查找一个名为该文件的文件。字符串替换发生在运行时,而require
发生在构建时。因此,当require运行时,它不会替换任何内容,它只会尝试查找名为该文件的文件。对于image source,如果要使用require
,则参数必须是原始字符串。你可以改为uri
。查看此链接,因为已经给出了答案-谢谢回复,我现在就知道了!对于图像源,如果要使用require
,则参数必须是原始字符串。你可以改为uri
。查看此链接,因为已经给出了答案-谢谢回复,我现在就知道了!