我有一个代码来获取请求。它正在编译和工作,但我在控制台中出错。
public get<T>(url: string, headers: Headers = this.jsonHeaders()): Observable<T> {
return this._http.get(url, { headers: headers })
.catch((err: Response, caught: Observable<T>) => {
if (err.status === 401) {
this._router.navigate(["/auth/login"]);
return Observable.throw("401 Unauthorized");
}
return caught;
})
.map(res => <T>this.toJSON(res));
}
错误:
error TS2345: Argument of type '(err: Response, caught: Observable<T>) => ErrorObservable | Observable<T>' is not assignable to parameter of type '(err: any, caught: Observable<Response>) => Observable<any>'.
Types of parameters 'caught' and 'caught' are incompatible.
Type 'Observable<Response>' is not assignable to type 'Observable<T>'.
Type 'Response' is not assignable to type 'T'.
我试过把它投到但是没有帮助
答案 0 :(得分:9)
您使用以下内容:
.catch((err: Response, caught: Observable<T>) => {
if (err.status === 401) {
this._router.navigate(["/auth/login"]);
return Observable.throw("401 Unauthorized");
}
return Observable.throw(caught); // <-----
})