我有一个方法handleError(),如文档https://angular.io/docs/ts/latest/guide/server-communication.html#!#error-handling
private handleError(error: any) {
console.error(error);
console.log(this.loginService); // <- always undefined
return Observable.throw(error);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,this.loginService是未定义的,尽管它已在我的类中正确注入.它已经在其他方法中使用,但似乎在handleError中不可用.
http-catch调用方法的方式可能是问题吗?如果是这样,我怎么能绕过那个?处理错误时我需要执行一些逻辑.
这是一个关于如何将handleError方法设置为回调的示例(与文档完全一样)
this.http.get(url,
ApiRequest.ACCEPT_JSON)
.map(ApiHelper.extractData)
.catch(this.handleError);
Run Code Online (Sandbox Code Playgroud)