RXJS 6处理http Observable的方式是什么?

时间:2019-06-13 13:29:58

标签: angular rxjs

我注意到当我在新项目中使用它时,我的代码不起作用:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

import { TypeAndCountURL } from '@app/constants';
import { HttpErrorService } from '@app/services/http-error.service';
import { TypeAndCountResponseBody, TypeAndCountRequestBody } from '@app/models/type-and-count/bodies.interface';

@Injectable({
  providedIn: 'root'
})
export class ApiService {
  constructor (private http: HttpClient, private httpErrorService: HttpErrorService) {}

  postTypeAndCountRequest(typeAndCountRequestBody: TypeAndCountRequestBody): Observable<TypeAndCountResponseBody> {
    return this.http.post<TypeAndCountResponseBody>(TypeAndCountURL, typeAndCountRequestBody).pipe(
      catchError(this.httpErrorService.handleError<TypeAndCountResponseBody>('postTypeAndCountRequest'))
    );

  }
}

具体地说,我得到Cannot find name 'catchError'. Did you mean 'RTCError'?ts(2552)

继续阅读,我发现我可以通过将其单独导入(从rxjs / operators .. whihc很好,但是)..来解决该问题,而且整个结构都是rxjs 5的兼容模式... RXJS 6处理错误响应的方式是什么?

1 个答案:

答案 0 :(得分:4)

尝试这样导入

import { catchError } from "rxjs/operators";

是否更新了类似的内容?

fetchUser() {
   this.userService.getProfile()
      .subscribe(
         (data: User) => this.userProfile = { ...data }
          , // success path
          error => this.error = error // error path
   );
}