I am receiving the following strange dependency injection behavior when using custom HttpInterceptors in angular 5+.
The following simplified code works fine:
you need to add Injector
into constructor and inject AuthService
via injector
export class AuthInterceptor implements HttpInterceptor {
constructor(private inj: Injector) {}
intercept(req: HttpRequest, next: HttpHandler): Observable> {
const auth = this.inj.get(AuthService);
const token = this.auth.getToken();
return next.handle(req);
}
}
don't forget import
import {Injector} from '@angular/core';