【发布时间】:2018-01-21 02:26:38
【问题描述】:
我尝试创建一个 HttpInterceptor 来为发生的每个 http 添加一些用于授权的标头。我需要从名为 AuthService 的服务中获取标头。下面是代码:
拦截器:
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) { }
}
授权服务:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class AuthService {
constructor(private http: HttpClient) { }
}
应用模块:
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
}, AuthService]
我收到以下错误:
错误:提供程序解析错误: 无法实例化循环依赖! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"):在 NgModule AppModule 中 在 ./AppModule@-1:-1
我已经检查了以前的答案,但我不明白在哪里检测到循环依赖。这里描述了我想要做的事情:https://angular.io/guide/http#setting-new-headers
【问题讨论】:
标签: angular