Javascript “如何修复”;Can';t查找变量:item";
我正在构建一个简单的图像列表应用程序,它有一些额外的功能,其中之一就是下载图像功能 然而,我一直得到这个“找不到变量项”错误。我该如何解决这个问题Javascript “如何修复”;Can';t查找变量:item";,javascript,react-native,expo,Javascript,React Native,Expo,我正在构建一个简单的图像列表应用程序,它有一些额外的功能,其中之一就是下载图像功能 然而,我一直得到这个“找不到变量项”错误。我该如何解决这个问题 item.image /。。。 下载图像=()=>{ const fileUri=`${FileSystem.documentDirectory}惊人的.jpg` 文件系统 .downloadAsync(item.image,fileUri) .然后({uri})=>{ console.log('已完成下载到',uri) }) CameraRoll.
item.image
/。。。
下载图像=()=>{
const fileUri=`${FileSystem.documentDirectory}惊人的.jpg`
文件系统
.downloadAsync(item.image,fileUri)
.然后({uri})=>{
console.log('已完成下载到',uri)
})
CameraRoll.saveToCameraRoll(fileUri,'photo')
}
// ...
返回(
// ...
// ...
);
更新
renderItem=({item})=>{
返回(
{
this.toggleModal();
这是我的国家({
webViewurl:item.image
});
}}
onLongPress={()=>Linking.openURL(item.image)}
activeColor=“蓝色”
>
);
}
您没有声明变量项
/。。。
下载图像=()=>{
常量项={}
const fileUri=`${FileSystem.documentDirectory}惊人的.jpg`
文件系统
.downloadAsync(item.image,fileUri)
.然后({uri})=>{
log('已完成下载到',uri);
})
CameraRoll.saveToCameraRoll(fileUri,'photo');
}
// ...
您可以这样做尝试使用“this”关键字:
FileSystem.downloadAsync(
this.item.image,
fileUri
)
未声明项变量。所以你不能用它。 应将renderItem函数中的项绑定到downLoadImage函数,如下所示:
renderItem=(项目)=>{
返回(
//一些代码
//一些代码
)
}
下载图像=(项目)=>{
//对这个项目做些什么
}
这只会更改错误消息,但没有帮助,因为该问题是您在何处声明项的另一个问题。item
是从githubIsitem
上托管的json文件中获取的,该文件是在其他地方定义的?它是同一类的成员变量吗?请提供更多代码或尝试找出item
的位置。item
是从Github上托管的json文件中获取的,那么您需要在调用它时将其提供给downloadImage
,或者使其成为您正在使用的类的成员?可悲的是,没有看到更多的代码,我无法说出正确的解决方案…我的代码是错误的。这有帮助吗?您提供的代码实际上没有帮助您了解问题的确切位置。请使用构建一个工作示例,以便能够分析代码并发现问题。也许现在你自己已经发现了问题undefined不是一个对象(评估“\u this.item.image”)
它可以正常工作,但现在它给出了“expo-mable unhandled promise拒绝打开失败:eNot-no-this.item.image”
renderItem = ({ item }) => {
return (
<View style={{ padding: 15, paddingBottom: 0 }}>
<Card elevation={1}>
<View
style={{
flex: 1,
flexDirection: "row",
flexWrap: "wrap",
alignItems: "flex-start"
}}
>
<View style={{ flex: 1 }}>
<TouchableOpacity
onPress={() => {
this.toggleModal();
this.setState({
webViewurl: item.image
});
}}
onLongPress={() => Linking.openURL(item.image)}
activeColor="blue"
>
<ImageBackground
source={{ uri: item.image }}
style={{ height: 216 }}
>
<IconButton
icon="favorite-border"
size={20}
color="#6200EE"
style={{ alignSelf: "flex-end" }}
onPress={this._savedAlert}
/>
</ImageBackground>
</TouchableOpacity>
</View>
</View>
</Card>
</View>
);
}
FileSystem.downloadAsync(
this.item.image,
fileUri
)