简体   繁体   中英

TS2339:(TS) Property 'catch' does not exist on type 'Observable<Object>'

My code which i tried to impliment, it shows 'catch' does not exist.

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { User } from '../Model/User';
import { environment } from '../../environments/environment';
import 'rxjs/add/operator/catch';  

@Injectable()
export class UserService {
  headers: HttpHeaders;
  constructor(private httpClient: HttpClient) {
    this.headers = new HttpHeaders({ 'content-type': 'application/json' });
  }
  GetUser(): Observable<User[]> {
    return this.httpClient.get(environment.apiAddress + '/user').catch(err => Observable.throw(err));
  }
}

You need to pipe the Observable.

GetUser(): Observable<User[]> {
  return this.httpClient.get(environment.apiAddress + '/user').pipe(
    catchError(err => throw(err)),
  );
}

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