Próbowałem utworzyć HttpInterceptor, aby dodać nagłówki autoryzacji do każdego http, który się dzieje. Potrzebuję pobrać nagłówki z usługi o nazwie AuthService. Oto poniżej kod:Błąd wtryskiwacza "Błąd analizy dostawcy: nie można utworzyć wystąpienia zależności cyklicznej!"
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 pojawia się następujący błąd:
Error: Provider parse errors: Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./[email protected]:-1
Sprawdziłem już poprzednie odpowiedzi, ale nie rozumiem, gdzie wykryto cykliczną zależność. Co próbuję zrobić jest opisany tutaj: https://angular.io/guide/http#setting-new-headers
Bardzo ładna i czysta odpowiedź, wielkie dzięki! :) – dave0688