简体   繁体   中英

What is the RXJS 6 way of handling http Observables?

I noticed that my code doesnt work when I use it in a new project:

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'))
    );

  }
}

specifically, I am getting Cannot find name 'catchError'. Did you mean 'RTCError'?ts(2552) Cannot find name 'catchError'. Did you mean 'RTCError'?ts(2552)

Reading up on this, I see that I can resolve the issue by importing it separately (from rxjs/operators .. whihc is fine, but) .. but also that this whole structure is compat mode for rxjs 5 ... what is the RXJS 6 way of handling the error response?

You can use

import { catchError } from "rxjs/operators";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM