angular 2 - Injected service in http error handler

前端 未结 3 518
广开言路
广开言路 2020-12-06 14:16

I have a method handleError() like in the documentation https://angular.io/docs/ts/latest/guide/server-communication.html#!#error-handling

private handleErro         


        
3条回答
  •  醉话见心
    2020-12-06 14:36

    Since you're passing the function directly, you don't have the this context of your class in there. A really easy and best practice way would be to use a lambda or "fat arrow function":

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

    A really good read on when to use lambdas: https://stackoverflow.com/a/23045200/1961059

提交回复
热议问题