I tried creating an HttpInterceptor to add some authorization headers to every http that happens. I need to get the headers from the AuthService . Here is the code below:
interceptor:
import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { AuthService } from './auth.service'; @Injectable() export class AuthInterceptor implements HttpInterceptor { constructor(private auth: AuthService) { } }
AuthService:
import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; @Injectable() export class AuthService { constructor(private http: HttpClient) { } }
AppModule:
providers: [{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true, }, AuthService]
I get the following error:
Error: Provider Analysis Errors: Unable to create circular dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR β]"): in NgModule AppModule in./ AppModule@-1 : -1
I already checked the previous answers, but I donβt understand where the cyclic dependency was discovered. What I'm trying to do is described here: https://angular.io/guide/http#setting-new-headers
source share