我能够从我在 RN 中创建的本地 JSON 文件动态引入数据。但是,当我返回并为每个配置文件插入图像以便可以随其配置文件信息动态显示时,应用程序中断并显示“该组件不能包含子组件。如果您想在图像顶部呈现内容,请考虑使用”。
我已经为所有个人资料使用了背景,所以我真的不明白它在说什么,因为我在 JSON 文件中输入的图像将是每个 Angular 色的个人资料图像。我是否遵循正确的格式来调用图像?任何帮助,将不胜感激。
这就是我设置 JSON 文件的方式:
const characters = [
{ id: "1", name: "Homer Simpson", occupation: "Nuclear Safety Inspector",
url:
"https://assets.fxnetworks.com/cms/prod/shows/the-simpsons/photos/simpsons-sidebar/character-facts/Homer/swsb_character_fact_homer_288x763.png"
},
{ id: "2", name: "Marge Simpson", occupation: "Stay-at-home mom", url:
"https://assets.fxnetworks.com/cms/prod/shows/the-simpsons/photos/simpsons-sidebar/character-facts/Homer/swsb_character_fact_homer_288x763.png"},
{ id: "3", name: "Bart Simpson", occupation: "Student", url:
"https://assets.fxnetworks.com/cms/prod/shows/the-simpsons/photos/simpsons-sidebar/character-facts/Homer/swsb_character_fact_homer_288x763.png" },
{ id: "4", name: "Lisa Simpson", occupation: "Student", url:
"https://assets.fxnetworks.com/cms/prod/shows/the-simpsons/photos/simpsons-sidebar/character-facts/Homer/swsb_character_fact_homer_288x763.png" },
{ id: "5", name: "Maggie Simpson", occupation: "Baby", url:
"https://assets.fxnetworks.com/cms/prod/shows/the-simpsons/photos/simpsons-sidebar/character-facts/Homer/swsb_character_fact_homer_288x763.png" }
export default characters;
这就是我引入它的方式:
import React from "react";
import {
View,
Text,
StyleSheet,
Image,
Button,
ImageBackground
} from "react-native";
class CharacterProfiles extends React.Component {
static navigationOptions = {
title: "The Simpsons",
headerStyle: {
backgroundColor: "#53b4e6"
},
headerTintColor: "#f6c945",
headerTitleStyle: {
fontWeight: "bold"
}
};
render() {
const { item } = this.props.navigation.state.params;
return (
<View>
<ImageBackground
source={{
uri:
"https://backgrounddownload.com/wp-content/uploads/2018/09/simpsons-clouds-background-5.jpg"
}}
style={{ width: "100%", height: "100%" }}
>
<Image>{item.url}</Image>
<Text>Name: {item.name}</Text>
<Text>Occupation: {item.occupation}</Text>
<Button
title="Character Directory"
onPress={() => this.props.navigation.navigate("CharacterDirectory")}
/>
<Button
title="Homepage"
onPress={() => this.props.navigation.navigate("Home")}
/>
</ImageBackground>
</View>
);
}
}
export default CharacterProfiles;
最佳答案
与您对 ImageBackground
所做的操作相同组件,您需要使用source
属性来分配 Image
的 URL 。您可以在 React Native 的 Image component documentation 中查看更详细的示例。 .
<Image
source={{uri: item.url}}
/>
在 <Image>
的内容中包含源 URL标记为文本将不起作用。这样做本质上相当于在 HTML 中编写以下内容:
<img>https://example.com/myImage.png</img>
您收到有关将内容放置在图像顶部的警告,因为 React Native 认为您正在尝试将 URL作为文本叠加在图像上。自 HTML <img>
elements 不能这样做,Image
也不能这样做。组件。
关于javascript - 尝试从本地 JSON 文件调用图像时,图像不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728407/