对 `require` 的调用只期望 1 个字符串文字参数,但发现了这一点:
calls to `require` expect exactly 1 string literal argument, but this was found:
我在应用程序中有一个带有 SectionList 的组件,它从本地 json 文件解析其内容。列表项有图像和文本,当需要使用 varibale 的图像时会导致错误:calls to require expect exactly 1 string literal argument, but this was found: item.logo
.
我的 json 文件
{
"algorithms":[
{"title":"Sorting algorithms","logo":"../logos/sorting.png"},
{"title":"Graph theory","logo":"../logos/graph.png"},
{"title":"Strings","logo":"../logos/strings.png"},
{"title":"Greedy technique","logo":"../logos/greedy.png"},
{"title":"Dynamic programming","logo":"../logos/dp.png"}
],
"datastructures":[
{"title":"Trees","logo":"../logos/tree.png"},
{"title":"Lists","logo":"../logos/list.png"},
{"title":"Tries","logo":"../logos/trie.png"},
{"title":"Stack and Queue","logo":"../logos/stack.png"},
{"title":"Hash Tables","logo":"../logos/hashtable.png"}
]
}
和renderItem
函数
renderItem = ({item}) => {
return(
<TouchableWithoutFeedback onPress={()=>this.props.navigation.navigate('List')}>
<View style={styles.itemHolder}>
<Image resizeMode="center" style={styles.itemLogo} source={require(item.logo)}/>
<View style = {styles.itemNameHolder}>
<Text style = {styles.itemName}>{item.title}</Text>
</View>
<View style={styles.itemPointer}>
<Image style={{width:50,height:50,}} source={require('../icons/right-arrow.png')}/>
</View>
<Divider/>
</View>
</TouchableWithoutFeedback>
);
}
已编辑
js 中的函数require
不能动态运行。它的参数必须是字符串。
编辑:
我认为您需要使用 map
的解决方法
首先你得想办法把logo格式弄成这样
const data = [
{"title":"Sorting algorithms","logo": require('../logos/sorting.png')},
{"title":"Graph theory","logo": require('../logos/graph.png')},
]
然后当你想使用它的时候就这样做
render() {
return (
<View>
{data.map((item) => {
<View>
<Image
style={styles.image}
source={item.logo}
/>
</View>
})}
</View>
);
}
让我知道它是否有效
我在应用程序中有一个带有 SectionList 的组件,它从本地 json 文件解析其内容。列表项有图像和文本,当需要使用 varibale 的图像时会导致错误:calls to require expect exactly 1 string literal argument, but this was found: item.logo
.
我的 json 文件
{
"algorithms":[
{"title":"Sorting algorithms","logo":"../logos/sorting.png"},
{"title":"Graph theory","logo":"../logos/graph.png"},
{"title":"Strings","logo":"../logos/strings.png"},
{"title":"Greedy technique","logo":"../logos/greedy.png"},
{"title":"Dynamic programming","logo":"../logos/dp.png"}
],
"datastructures":[
{"title":"Trees","logo":"../logos/tree.png"},
{"title":"Lists","logo":"../logos/list.png"},
{"title":"Tries","logo":"../logos/trie.png"},
{"title":"Stack and Queue","logo":"../logos/stack.png"},
{"title":"Hash Tables","logo":"../logos/hashtable.png"}
]
}
和renderItem
函数
renderItem = ({item}) => {
return(
<TouchableWithoutFeedback onPress={()=>this.props.navigation.navigate('List')}>
<View style={styles.itemHolder}>
<Image resizeMode="center" style={styles.itemLogo} source={require(item.logo)}/>
<View style = {styles.itemNameHolder}>
<Text style = {styles.itemName}>{item.title}</Text>
</View>
<View style={styles.itemPointer}>
<Image style={{width:50,height:50,}} source={require('../icons/right-arrow.png')}/>
</View>
<Divider/>
</View>
</TouchableWithoutFeedback>
);
}
已编辑
js 中的函数require
不能动态运行。它的参数必须是字符串。
编辑: 我认为您需要使用 map
的解决方法首先你得想办法把logo格式弄成这样
const data = [
{"title":"Sorting algorithms","logo": require('../logos/sorting.png')},
{"title":"Graph theory","logo": require('../logos/graph.png')},
]
然后当你想使用它的时候就这样做
render() {
return (
<View>
{data.map((item) => {
<View>
<Image
style={styles.image}
source={item.logo}
/>
</View>
})}
</View>
);
}
让我知道它是否有效