将参数图像传递到一个文件在手机上不起作用,但在 PC 上起作用
**我有 2 个文件,一个称为firstFile,另一个称为secondFile,我想在按下按钮时将图像从firstFile 渲染到secondFile 中。它可以在桌面上运行,但不能在手机上运行,而是向我显示开发服务器返回了响应错误代码:500 &&第 18 行无效调用:require(“../assets/img/” + image) ** 如果有人可以帮助我,那就太好了,谢谢
///. firstFile file
function firstFile() {
const navigation = useNavigation(); // for navigation on press purpose.
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 1 }}>
<View style={{ height: 150, marginTop: 10 }}>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
>
<Pressable onPress={() => navigation.navigate("secondFile", {
image:"Tree-character_SVG.png",
text:"Get green\nelectricity",
})}>
<Category
imageUri={require("../assets/img/Tree-character_SVG.png")}
text="Get green\nelectricity"
/>
</Pressable>
</ScrollView>
</View>
</View>
</View>
</SafeAreaView>
);
}
export default firstFile;
// secondFile file
export default function secondFile({route}) {
const navigation = useNavigation(); // for navigation on press purpose.
const { image, text} = route.params;
return (
<View style={styles.container}>
<View style={styles.imageContainer}>
<Image
style={styles.image}
source={require(`../assets/img/${image}`)}
/>
<Text style={styles.imageText}>{text}</Text>
</View>
</View>
);
}
**I have 2 files, one is called firstFile and other is secondFile, I want to render the image from firstFile into secondFile on press button. and it is working on the desktop but not working on phone instead it is showing me the development server returned response errror code : 500 && Invalid call at line 18: require(“../assets/img/” + image) **
if anybody can help me, it would be great relieve thank you
///. firstFile file
function firstFile() {
const navigation = useNavigation(); // for navigation on press purpose.
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 1 }}>
<View style={{ height: 150, marginTop: 10 }}>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
>
<Pressable onPress={() => navigation.navigate("secondFile", {
image:"Tree-character_SVG.png",
text:"Get green\nelectricity",
})}>
<Category
imageUri={require("../assets/img/Tree-character_SVG.png")}
text="Get green\nelectricity"
/>
</Pressable>
</ScrollView>
</View>
</View>
</View>
</SafeAreaView>
);
}
export default firstFile;
// secondFile file
export default function secondFile({route}) {
const navigation = useNavigation(); // for navigation on press purpose.
const { image, text} = route.params;
return (
<View style={styles.container}>
<View style={styles.imageContainer}>
<Image
style={styles.image}
source={require(`../assets/img/${image}`)}
/>
<Text style={styles.imageText}>{text}</Text>
</View>
</View>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你能试试这个代码吗?
could you try this code?
在 RN 中,你的 require 中不能有变量。
尝试在第一个文件 const image = require'//full require'
,然后通过 props 传递图像,然后你就可以直接使用 source={image}
You cant have variables in your require in RN.
Try in the first file const image = require'//full require'
and then pass image through props and then you can just go source={image}