在Angular 6+ httpClient中,可以配置请求以获取整个响应。
可观察到的响应可以通过管道传输到map
和catchError
运营商。
什么时候执行通过map
运算符和什么时候到catchError
?
它取决于响应状态代码吗?
例如,如果response.status === 200
然后去map
,否则去catchError
?
如果不仅状态200进入map
,那么还有哪些?
哪些状态去catchError
?
getData(): Observable<[]> {
return this.http.get(this.apiUrl, {observe: 'response'}).pipe(
map((response: HttpResponse<any>) => {
return response.status === 200;
}),
catchError((errorResponse: HttpErrorResponse) =>
// which value may be logged here?
console.log(errorResponse.status);
of(false);
));
}
4xx和5xx状态代码是错误。其他的都是成功的。