在 angular 中使用 RxJS 运算符转换响应数据
Transform response data with RxJS Operators in angular
我有如下服务响应。
"amount": 3330,
"count": 56,
"dataObjects": [
{
"links": {
"self": "http://localhost:4200/api/collection/"
},
"id": "W391",
"objectNumber": "IN-W391",
"title": "Image 23",
"webImage": {
"guid": "89de22aa-e19f-4c83-87ff-26dd8f319c05",
"width": 1200,
"height": 800,
"url": "http://localhost:4200/api/collection/7qzT0pbclLB7y3fdS1GxzMnV7m3gD3gWnhlquhFaJSn6gNOvMmTUAX3wVlTzhMXIs8kM9IH8AsjHNVTs8em3XQI6uMY=s0"
}
},
{
"links": {
"self": "http://localhost:4200/api/collection/"
},
"id": "W391",
"objectNumber": "IN-W391",
"title": "Image 23",
"webImage": {
"guid": "89de22aa-e19f-4c83-87ff-26dd8f319c05",
"width": 1200,
"height": 800,
"url": "http://localhost:4200/api/collection/7qzT0pbclLB7y3fdS1GxzMnV7m3gD3gWnhlquhFaJSn6gNOvMmTUAX3wVlTzhMXIs8kM9IH8AsjHNVTs8em3XQI6uMY=s0"
}
}
]
}
我想做的就是只检索 dataObjects 数组,这样我就可以遍历它并绑定 url 在 webImage 下显示在 UI 中。这是我试过的
Component.ts
ngOnInit() {
this.artistService.GetArtistDetails().pipe(map( response=> response.dataObjects))
.subscribe((artists: any[])=>{
console.log(artists);
});
}
}
Service.ts
public GetArtistDetails(){
return this.httpClient.get(this.REST_API_SERVER);
}
提前致谢
除了 A2Ia 所说的,这里还有一个示例代码可以正常工作:
stackblitz 应用:https://stackblitz.com/edit/angular-ivy-1ixvmx?file=src/app/app.component.ts
我有如下服务响应。
"amount": 3330,
"count": 56,
"dataObjects": [
{
"links": {
"self": "http://localhost:4200/api/collection/"
},
"id": "W391",
"objectNumber": "IN-W391",
"title": "Image 23",
"webImage": {
"guid": "89de22aa-e19f-4c83-87ff-26dd8f319c05",
"width": 1200,
"height": 800,
"url": "http://localhost:4200/api/collection/7qzT0pbclLB7y3fdS1GxzMnV7m3gD3gWnhlquhFaJSn6gNOvMmTUAX3wVlTzhMXIs8kM9IH8AsjHNVTs8em3XQI6uMY=s0"
}
},
{
"links": {
"self": "http://localhost:4200/api/collection/"
},
"id": "W391",
"objectNumber": "IN-W391",
"title": "Image 23",
"webImage": {
"guid": "89de22aa-e19f-4c83-87ff-26dd8f319c05",
"width": 1200,
"height": 800,
"url": "http://localhost:4200/api/collection/7qzT0pbclLB7y3fdS1GxzMnV7m3gD3gWnhlquhFaJSn6gNOvMmTUAX3wVlTzhMXIs8kM9IH8AsjHNVTs8em3XQI6uMY=s0"
}
}
]
}
我想做的就是只检索 dataObjects 数组,这样我就可以遍历它并绑定 url 在 webImage 下显示在 UI 中。这是我试过的
Component.ts
ngOnInit() {
this.artistService.GetArtistDetails().pipe(map( response=> response.dataObjects))
.subscribe((artists: any[])=>{
console.log(artists);
});
}
}
Service.ts
public GetArtistDetails(){
return this.httpClient.get(this.REST_API_SERVER);
}
提前致谢
除了 A2Ia 所说的,这里还有一个示例代码可以正常工作:
stackblitz 应用:https://stackblitz.com/edit/angular-ivy-1ixvmx?file=src/app/app.component.ts