核心模块中的Angular HttpInterceptor和AccountService-多个实例

时间:2019-04-21 07:08:16

标签: angular

我已经在Angular 7中创建了CoreModule

核心模块包含AccountServiceHttpInterceptor服务:

providers: [..., AccountService,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: TokenInterceptor,
      multi: true
    }]

这是AccountService的代码:

constructor(private http: HttpClient,
    @Inject('BASE_URL') public baseUrl: string,
    private router: Router,
    private notifierSrv: NotifierService) {
    ...
    console.log('new acc srv');
    ...
  }

AccountService被注入到HttpInterceptor服务:

@Injectable()
export class TokenInterceptor implements HttpInterceptor {
    constructor(private accountSrv: AccountService) {
        console.log(`TokenInterceptor init with acc: ${accountSrv}`);
    }
...

主要AppModule导入CoreModule

问题:在应用的首次加载期间,我在浏览器控制台(Chrome)中看到约300项服务创建 Browser Console Log

enter image description here

也有例外:

account.service.ts:43 RangeError: Maximum call stack size exceeded

注入器应将其创建为单例。我想念什么?

0 个答案:

没有答案