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

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


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 处理错误响应的方式?


萧十郎
浏览 130回答 1
1回答

BIG阳

您可以使用import { catchError } from "rxjs/operators";fetchUser() {&nbsp; &nbsp; &nbsp; &nbsp;this.userService.getProfile()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subscribe(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(data: User) => this.userProfile = { ...data }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , // success path&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error => this.error = error // error path&nbsp; &nbsp; &nbsp; &nbsp;);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript