相关疑难解决方法(0)

angular 2 - http错误处理程序中的注入服务

我有一个方法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)

angular2-injection angular2-http angular

5
推荐指数
2
解决办法
3593
查看次数