Tôi đã cố gắng tạo một HttpInterceptor để thêm một số tiêu đề để ủy quyền cho mọi http xảy ra. Tôi cần lấy các tiêu đề từ một dịch vụ có tên là AuthService. Đây là đoạn mã dưới đây:Lỗi Injector "Lỗi phân tích cú pháp của nhà cung cấp: Không thể khởi tạo phụ thuộc tuần hoàn!"
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]
tôi nhận được lỗi sau:
Error: Provider parse errors: Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./[email protected]:-1
Tôi đã kiểm tra các câu trả lời trước nhưng tôi không hiểu nơi phát hiện sự phụ thuộc chu kỳ. Những gì tôi đang cố gắng làm được mô tả ở đây: https://angular.io/guide/http#setting-new-headers
Câu trả lời rất hay và rõ ràng, cảm ơn rất nhiều! :) – dave0688