我正在实现应用初始化程序,遇到如下循环依赖错误
未捕获的错误:提供程序解析错误: 无法实例化循环依赖! ApplicationRef(“ [ERROR->]”):在./AppModule@-1:-1中的NgModule AppModule中 在NgModuleProviderAnalyzer.push ../ node_modules/@angular/compiler/fesm5/compiler.js.NgModuleProviderAnalyzer.parse(compiler.js:11472) 在NgModuleCompiler.push ../ node_modules/@angular/compiler/fesm5/compiler.js.NgModuleCompiler.compile(compiler.js:11836) 在JitCompiler.push ../ node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModule(compiler.js:23882) 在compile.js:23841 在Object.then(compiler.js:1007) 在JitCompiler.push ../ node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndComponents(compiler.js:23839) 在JitCompiler.push ../ node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler.compileModuleAsync(compiler.js:23799) 在CompilerImpl.push ../ node_modules/@angular/platform-browser-dynamic/fesm5/platform-browser-dynamic.js.CompilerImpl.compileModuleAsync(platform-browser-dynamic.js:143) 在PlatformRef.push ../ node_modules/@angular/core/fesm5/core.js.PlatformRef.bootstrapModule(core.js:4352) 在Object ../ src / main.ts(main.ts:11) push ../ node_modules/@angular/compiler/fesm5/compiler.js.NgModuleProviderAnalyzer.parse @ Compiler.js:11472 push ../ node_modules/@angular/compiler/fesm5/compiler.js.NgModuleCompiler.compile @ Compiler.js:11836 push ../ node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModule @ Compiler.js:23882 (匿名)@ Compiler.js:23841 然后@ compiler.js:1007 推送../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndComponents @ Compiler.js:23839 push ../ node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler.compileModuleAsync @ Compiler.js:23799 push ../ node_modules/@angular/platform-browser-dynamic/fesm5/platform-browser-dynamic.js.CompilerImpl.compileModuleAsync @ platform-browser-dynamic.js:143 push ../ node_modules/@angular/core/fesm5/core.js.PlatformRef.bootstrapModule @ core.js:4352 ./src/main.ts @ main.ts:11 webpack_require @引导程序:81 0 @ main.ts:12 webpack_require @引导程序:81 checkDeferredModules @ bootstrap:43 webpackJsonpCallback @引导程序:30 (匿名)@ main.js:1
这是我的初始化程序的样子
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http';
@Injectable()
export class InitializerService {
flag = false;
constructor(private http: HttpClient, private route: ActivatedRoute) { }
initialize(): boolean {
// access query params using ActivatedRoute object
// some http calls using HttpClient object
// also use objects of HttpHeaders and HttpParams
return this.flag;
}
}
下面是我如何在NgModule中提供服务
providers: [
InitializerService,
{ provide: APP_INITIALIZER, useFactory: init_app, deps: [InitializerService], multi: true},
{ provide: HTTP_INTERCEPTORS, useClass: Interceptor, multi: true }
],
我不确定为什么会看到周期性依赖错误
答案 0 :(得分:-1)
由于自举,ActivateRoute在APP_INITIALIZER上不可用。您可以使用APP_BOOTSTRAP_LISTENER InjectionToken来确保ActivatedRoute可用(在引导完成后发生)。