【发布时间】:2016-10-25 14:53:11
【问题描述】:
我有一个方法 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);
}
我的问题是,this.loginService 是 undefined,尽管它已正确注入我的班级。它已在其他方法中使用,但在 handleError 中似乎不可用。
http-catch 调用方法的方式可能是问题吗? 如果是这样,我该如何解决?处理错误时需要执行一些逻辑。
这是一个关于我如何将 handleError 方法设置为回调的示例(就像文档一样)
this.http.get(url,
ApiRequest.ACCEPT_JSON)
.map(ApiHelper.extractData)
.catch(this.handleError);
【问题讨论】:
-
您是否将
loginService传递给类构造函数? -
是的。我已经在同一个类的其他方法中使用它。
-
你能告诉我们你如何在你的捕获中调用
handleError吗? -
我更新了我的问题。
标签: angular angular2-http angular2-injection