我有一个REST API,在following structure中返回json数据-内容本身是inside a "data" node,因为有时会在请求中有额外的Meta信息(例如分页).
{
"data": [
{
"id": 1,
"title": "List 1"
},
{
"id": 2,
"title": "List 2"
},
]
}
我使用Angular http客户端获取数据,如下所示:
getLists (): Observable<List[]> {
return this._httpClient.get<List[]>('http://localhost/api/v1/lists').pipe(
tap((response) => {
this._lists.next(response)
}),
)
}
问题是--正如你可能已经知道的--response is not an array of elements of type "List"导致了elements are inside the "data" node.由于设置了泛型/预期返回类型,访问响应对象上的数据 node 内的内容也将不起作用.
有没有办法在get()方法上得到keep using the generic,然后得到access the content inside the "data" array,这样它就可以在List对象的数组中结束了?