所以我的问题很容易解释,我试图从此api https://www.youtube.com/watch?v=NT45HE7L7yk获取数据。现在一切正常,但是我不知道如何在此API中获取图片。当我尝试获取图片时,我得到的链接与图片本身相反。我想我需要以某种方式在循环内使用src标记,但我不确定如何实现。我将在下面发布我的代码,一如既往,感谢您的帮助,因为我喜欢学习更多有关react的知识。
import React, {Component} from 'react';
class App extends Component{
constructor(props){
super(props);
this.state = {
items:[],
isLoaded: false
};
}
componentDidMount(){
fetch('https://www.hatchways.io/api/assessment/students')
.then(res=> res.json())
.then(({ students }) => {
this.setState({
isLoaded: true,
items: students,
})
});
}
render(){
const {isLoaded, items} = this.state;
if(!isLoaded){
return <div>loading data...</div>;
}
else{
return(
<div className="Data">
<div>
{items.map(item=>(
<p key={item.id}>
name:{item.firstName +' '+ item.lastName +' '} |
City:{item.city} |
company:{item.company} |
email:{item.email} |
id:{item.id}|
picture:{item.pic}
</p>
))};
</div>
</div>
);
}
}
}
export default App;
答案 0 :(得分:1)
当我尝试获取图片时,我得到的链接与图片本身相反。
<p key={item.id}>
name:{item.firstName +' '+ item.lastName +' '} |
City:{item.city} |
company:{item.company} |
email:{item.email} |
id:{item.id}|
picture:{item.pic} <<<<<<< You just print it as text.
</p>
要显示URL中的图像,请使用img
标签。
<img src={item.pic}/>
答案 1 :(得分:0)
import React, {Component} from 'react';
class App extends Component{
constructor(props){
super(props);
this.state = {
items:[],
isLoaded: false
};
}
componentDidMount(){
fetch('https://www.hatchways.io/api/assessment/students')
.then(res=> res.json())
.then(({ students }) => {
this.setState({
isLoaded: true,
items: students,
})
});
}
render(){
const {isLoaded, items} = this.state;
if(!isLoaded){
return <div>loading data...</div>;
}
else{
return(
<div className="Data">
<div>
{items.map(item=>(
<p key={item.id}>
name:{item.firstName +' '+ item.lastName +' '} |
City:{item.city} |
company:{item.company} |
email:{item.email} |
id:{item.id}|
<img src={item.pic}/>
</p>
))};
</div>
</div>
);
}
}
}
export default App;