【发布时间】:2019-01-07 20:43:27
【问题描述】:
在 Angular 7 组件上,我正在调用服务:
this.postService.getByCategoryId(10)
.subscribe((response: GetPostsByCategoryIdResponse>) => {
this.posts = response.map((post: PostModel) => ({
id: response.id,
title: response.title,
category: response.category.name
}));
});
我正在将模仿 API 返回的数据结构的 GetPostsByCategoryIdResponse 映射到组件中使用的 PostModel。
PostService 的 GetByCategoryId 正在调用 API:
public GetByCategoryId(id: number): Observable<GetPostsByCategoryIdResponse> {
return this.httpClient.get<GetPostsByCategoryIdResponse>(url);
}
如何处理调用API时可能出现的错误?
【问题讨论】:
标签: angular angular6 angular7 angular-httpclient