angular 2 - Injected service in http error handler

前端 未结 3 513
广开言路
广开言路 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:54

    Possible solution is also to assign your service to static variable of class

    ClassToHandleError {
       private static loginService: LoginService;
    
       constructor(private loginService: LoginService) {
          ClassToHandleError.loginService = loginService;
       }
    
       private handleError(error: any) {
        console.error(error);
        console.log(ClassToHandleError.loginService); // here you can use static reference
        return Observable.throw(error);
       }
    }
    

    I know this is just workaround and rinukkusu provided definetely better solution then me. I used it until I get through this question. But maybe in some special case this will be valuable for somebody :) .

提交回复
热议问题