I try to implement an error service which need to use the Angular Router methods in this class. And all I can found is to insert the Router class into my service by using Injector method of Angular.
So, I do grab the Injector object with dependency injection in my constructor of my serice class:
Import { ErrorHandler, Injectable, Injector } from '@angular/core';
import { Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class RequestErrorHandlerService implements ErrorHandler {
private _router: any
constructor(
private injector: Injector
) {}
handleError(error) {
const router = this.injector.get(Router);
...
router.navigate(['/login']);
}
}
And I try to use injector.get() for to get the Router object. But all I get is an error
Uncaught TypeError: this.injector.get is not a function
So, I think I forgot something. But didn´t get the problem. Why does this not works?
Do have somebody a tip for me, or an link to a tutorial which realy works with Angular Version 8 or higher?
Thanks.