[英]Angular 7: How to properly this.http.get from a nested response?
我正在做Angular 7教程,想知道如果它们不在顶层而是嵌套的 ,如何正确地请求项目列表 :
顶层(作品)
[
{ id: 123, name: 'Name 123'},
{ id: 124, name: 'Name 124'},
// ...
]
嵌套(无效)
{
message: 'x items found.',
data: [
{ id: 123, name: 'Name 123'},
{ id: 124, name: 'Name 124'},
// ...
]
}
服务
// ...
@Injectable({ providedIn: 'root' })
export class ItemService {
private apiUrl = 'api/items'; // items are in .data
constructor(private http: HttpClient) {}
getItems (): Observable<Item[]> {
// Here is the problem:
return this.http.get<Item[]>(this.apiUrl, {
pathToItems: 'data' // <- Pseudocode
});
}
}
零件
// ...
this.itemService.getItems()
.subscribe(items => this.items = items);
我可以在服务中使用subscribe()
并返回.data
的内容,但我认为应该有一个更简单的解决方案。 我需要保留返回类型( Item
数组)。
任何想法?
提前致谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.