相关疑难解决方法(0)

为什么用catchError处理错误而不是在Angular的订阅错误回调中处理

所以我通常会这样写我的http请求

服务

getData(){
  return this.http.get('url')
}
Run Code Online (Sandbox Code Playgroud)

零件

getTheData() {
  this.service.getData().subscribe(
    (res) => {
      //Do something
    }, 
    (err) => {
      console.log('getData has thrown and error of', err)
    })
Run Code Online (Sandbox Code Playgroud)

但是浏览Angular文档后,他们似乎在Service中将其格式化为

getHeroes(): Observable<Hero[]> {
  return this.http.get<Hero[]>(this.heroesUrl)
    .pipe(
      catchError(this.handleError('getHeroes', []))
    );
}
Run Code Online (Sandbox Code Playgroud)

这隐含的上行空间是什么,因为它对我来说似乎很冗长,而且我个人从不需要解决错误。

rxjs angular2-observables angular

5
推荐指数
1
解决办法
1515
查看次数

标签 统计

angular ×1

angular2-observables ×1

rxjs ×1