Angular 2 - Introduced service in HTTP error handler

I have a handleError () method, as in the documentation 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);
}

My problem is that this.loginService is undefined , although it was correctly injected into my class. It is already used in other methods, but does not seem to be available in handleError .

Can a problem call the http-catch method a problem? If so, how can I get around this? I need to follow some logic when handling the error.

This is an example of how I set the handleError method as a callback (just like the documentation)

this.http.get(url,
              ApiRequest.ACCEPT_JSON)
              .map(ApiHelper.extractData)
              .catch(this.handleError);
+3
2

, this . - " ":

this.http.get(url, ApiRequest.ACCEPT_JSON)
    .map(res => ApiHelper.extractData(res))
    .catch(err => this.handleError(err));

, lambdas: fooobar.com/questions/3197/...

+18

this handleError , , , .

:

this.http.get(url,
              ApiRequest.ACCEPT_JSON)
              .map(ApiHelper.extractData) 
              .catch(this.handleError.bind(this)); // <-- add .bind(this)
+4

Source: https://habr.com/ru/post/1015941/


All Articles