Angular 在同一模块中提供拦截器和依赖自定义服务时出现的问题

Angular 在同一模块中提供拦截器和依赖自定义服务时出现的问题,angular,interceptor,angular-httpclient,angular-providers,angular-httpclient-interceptors,Angular,Interceptor,Angular Httpclient,Angular Providers,Angular Httpclient Interceptors,我在AppModule代码段中注入了令牌处理拦截器 @NgModule({ 进口:[ 浏览器模块, HttpClientModule ], 声明:[AppComponent], 供应商:[ AuthService, { 提供:HTTP_拦截器, useClass:BasicTokenInterceptor, 多:真的 } ], 导出:[AppComponent], 引导:[AppComponent] }) 导出类AppModule{} 我的拦截器 @Injectable() 导出类BasicT

我在
AppModule
代码段中注入了令牌处理拦截器

@NgModule({
进口:[
浏览器模块,
HttpClientModule
],
声明:[AppComponent],
供应商:[
AuthService,
{
提供:HTTP_拦截器,
useClass:BasicTokenInterceptor,
多:真的
}
],
导出:[AppComponent],
引导:[AppComponent]
})
导出类AppModule{}
我的拦截器

@Injectable()
导出类BasicTokenInterceptor实现HttpInterceptor{
构造函数(私有身份验证:身份验证服务){}
...
接收误差

Uncaught Error: Provider parse errors:
Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
    at NgModuleProviderAnalyzer.webpackJsonp.../../../compiler/@angular/compiler.es5.js.NgModuleProviderAnalyzer.parse (compiler.es5.js:11684)
    at NgModuleCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.NgModuleCompiler.compile (compiler.es5.js:18497)
    at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileModule (compiler.es5.js:26825)
    at compiler.es5.js:26770
    at Object.then (compiler.es5.js:1679)
    at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileModuleAndComponents (compiler.es5.js:26768)
    at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler.compileModuleAsync (compiler.es5.js:26697)
    at PlatformRef_.webpackJsonp.../../../core/@angular/core.es5.js.PlatformRef_._bootstrapModuleWithZone (core.es5.js:4536)
    at PlatformRef_.webpackJsonp.../../../core/@angular/core.es5.js.PlatformRef_.bootstrapModule (core.es5.js:4522)
    at Object.../../../../../src/main.ts (main.ts:11)
如果我在intercept内部执行注入:

@Injectable()
导出类BasicTokenInterceptor实现HttpInterceptor{
auth:AuthService;
构造函数(私有_injector:injector){}
addToken(请求:HttpRequest

但是仍然没有成功。

您可以通过手动注入服务,这将有助于避免此类错误:

/* Other imports */
import { Injectable, Injector } from '@angular/core';
import { AuthService } from 'path/to/auth.service';

@Injectable()
export class BasicTokenInterceptor implements HttpInterceptor {
  constructor(private _injector: Injector) {
    setTimeout(() => {
      const _authService = this._injector.get(AuthService);
      /* Or store it to class property, as you like */
      _authService.callMethod();
      /* ... */
    });
  }
/* ... */
}

您是否仅在您检查的问题中提到的调用intercept时才尝试注入您的服务?如果是这种情况,您是否有相同的错误?@Orodan您能告诉我您正在谈论的用户评论吗?因为每次请求都会调用我的intercept。我已签出解决方案。但他们甚至在I中使用了两个标志intercept函数。寻找更干净的方法,因为这是一个常见的问题。来自@perusopersonale的解决方案。它在截取方法中获得所需的服务,而不是在构造函数中获得。是的,它甚至可以工作。我问的是更干净或推荐的方法。是的,它可以工作,即使在角度git问题中,它也被提到。但是用户
tarasbobak
提到它是一种可黑客攻击的方式。此外,如果我设置了
setTimeout
,则会调用
intercept
函数,即使authservice不可用。
/* Other imports */
import { Injectable, Injector } from '@angular/core';
import { AuthService } from 'path/to/auth.service';

@Injectable()
export class BasicTokenInterceptor implements HttpInterceptor {
  constructor(private _injector: Injector) {
    setTimeout(() => {
      const _authService = this._injector.get(AuthService);
      /* Or store it to class property, as you like */
      _authService.callMethod();
      /* ... */
    });
  }
/* ... */
}