将http客户端注入HTTP Interceptor Angular 4

时间:2017-12-03 13:51:08

标签: angular angular-http-interceptors

如何将http client注入HTTP Interceptor?每当我这样做时,它会抛出:Uncaught Error: Provider parse errors: Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1

我的interceptor看起来像是:

@Injectable()
export class CustomHttpInterceptor implements HttpInterceptor {

  constructor(private ehs: ErrorHandlerService,
              private localStorageService: LocalStorageService,
              private router: Router,
              private http: HttpClient
             ){}

1 个答案:

答案 0 :(得分:1)

你可以使用注射器

  constructor(inj: Injector) {
    this.auth = inj.get(AuthService)
  }

查看此示例: https://plnkr.co/edit/8CpyAUSJcCiRqdZmuvt9?p=preview

https://angular.io/api/core/Injector

关于这个问题的长篇大论:https://github.com/angular/angular/issues/18224