[英]error TS2339: Property 'catch' does not exist on type 'Observable<empInterface[]>'
[英]Property 'catch' does not exist on type 'Observable<IEmployee[]>'.ts(2339) Error is not resolved by adding import 'rxjs/add/operator/catch';
当我将鼠标悬停在.catch(this.errorHandler)
,我收到一条错误消息
类型'Observable'.ts(2339)上不存在属性'catch'
我无法将catch函数导入角型打字稿。
当我将鼠标悬停在.catch(this.errorHandler)
,我收到一条错误消息
类型'Observable'.ts(2339)上不存在属性'catch'
根据另一个堆栈文章: 属性'catch'在类型'Observable <any>'中不存在,我应该添加:
import 'rxjs/add/operator/catch'
我也尝试导入
import {Observable} from 'rxjs/Rx';
和
import { catchError } from 'rxjs/operators';
并使用catchError代替catch。
这些都不起作用
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { IEmployee } from './employee';
import { Observable, fromEventPattern } from 'rxjs';
import 'rxjs/add/operator/catch';
import {catchError} from "rxjs/operators"
import 'rxjs/add/observable/throw';
@Injectable({
providedIn: 'root'
})
export class EmployeeService {
private _url : string = "../assets/data/employees.json";
constructor(private http: HttpClient) { }
getEmployees(): Observable<IEmployee[]>{
return this.http.get<IEmployee[]>(this._url)
.catch(this.errorHandler)
}
errorHandler(error:HttpErrorResponse){
return Observable.throw(error.message ||"Server Error")
}
}
两个问题:
使用catchError
不能catch
与.pipe()一起使用
return this.http.get<IEmployee[]>(this._url) .pipe(catchError(this.errorHandler));
试试看(catchError和throwError):
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { IEmployee } from './employee';
import { Observable, fromEventPattern, throwError} from 'rxjs';
import {catchError} from "rxjs/operators"
@Injectable({
providedIn: 'root'
})
export class EmployeeService {
private _url : string = "../assets/data/employees.json";
constructor(private http: HttpClient) { }
getEmployees(): Observable<IEmployee[]>{
return this.http.get<IEmployee[]>(this._url)
.pipe(catchError(this.handleError));
}
handleError(error: HttpErrorResponse) {
//throwError instead of Observable.throw
return throwError(error.error.message ||"Server Error");
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.