2 回答
TA贡献1825条经验 获得超6个赞
有几种方法可以做到这一点:
其中之一是:使用 map 迭代数据并返回User类型的新对象
this.http.get('https://jsonplaceholder.typicode.com/users')
.subscribe((data :any) => {
this.users = data.map((d: any) => {
let user : User = {name :d.name , email :d.email };
return user;
});
});
TA贡献1827条经验 获得超7个赞
您的 API 返回的不仅仅是电子邮件和姓名:
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
因此,我可能会将您的数据映射到每个属性,以便您确切地知道从输出中提取的内容:
this.data = data.map((user: any): User => {
return {
name: user.name,
email: user.email
}
});
添加回答
举报