Angular 5:导入OAuthModule.forRoot()时无法实例化循环依赖

时间:2019-07-26 06:07:49

标签: angular

我想将OAuthModule.forRoot()导入到我的项目中,但是抛出了以下错误:

  

compiler.js:19390未捕获的错误:提供程序解析错误:       无法实例化循环依赖! InjectionToken_HTTP_INTERCEPTORS(“ [ERROR->]”):在NgModule AppModule中   在./AppModule@-1:-1中           在NgModuleProviderAnalyzer.parse(compiler.js:19390)           在NgModuleCompiler.compile(compiler.js:19972)           在JitCompiler._compileModule(compiler.js:33574)           在评估时(compiler.js:33505)           在Object.then(compiler.js:455)           在JitCompiler._compileModuleAndComponents(compiler.js:33503)           在JitCompiler.compileModuleAsync(compiler.js:33419)           在CompilerImpl.compileModuleAsync(platform-b​​rowser-dynamic.js:230)           在PlatformRef.bootstrapModule(core.js:5447)           在评估时(main.ts:12)

找到导入并提供以下项目中使用的导入。

  imports: [ // import Angular's modules
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    ModalModule,
    HttpClientModule,
    HttpModule,
    TableModule,
    SliderModule,
    DropdownModule,
    MultiSelectModule,
    ModalModule.forRoot(),
    CoreModule,
    SmartadminLayoutModule,
    routing,
    PaginatorModule,
    DialogModule,
    ConfirmDialogModule,
    NgxInactivity,
    NgbModule.forRoot(),
    NgIdleKeepaliveModule.forRoot(),
    Ng4LoadingSpinnerModule.forRoot(),
    NgSelectModule,
    SmartadminModule,
    NotesModule,
    AttachmentsModule,
    CalendarModule,
    OAuthModule.forRoot()
  ],

使用的提供者:

providers: [ 
    // ENV_PROVIDERS,
    APP_PROVIDERS,
    OrganizationService,
    RecoveryService,
    AuthenticationService,
    HomeService,
    UserConfigurationService,
    RoleRightsService,
    NewsService,
    DashboardService,
    CurrencyService,
    EventTypeService,
    DetailEventTypeService,
    CountryService,
    InvoiceModeService,
    EquipmentSizeService,
    EquipmentTypeService,
    RecoveryDecisionService,
    RegionService,
    StateService,
    ExchangeRateService,
    ThirdPartyService,
    CanDeactivateGuard,
    CanActivateGuard,
    ConfirmationService,
    AttachmenttypeService,
    CityService,
    ExcelService,
    InvoiceService, 
    PDFService, 
    ShippingCompanyService,
    ThirdPartyTypeService,
    NotificationService,
    DownloadService,
    ConfigurationService,
    ReportParameterService,
    DateService,    
  ],
  schemas: [
    CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA
  ]
})
export class AppModule {
  constructor(public appRef: ApplicationRef, public appState: AppState) {

  }
}

1 个答案:

答案 0 :(得分:0)

我通过延迟加载OAuthService解决了该问题。无论在何处使用它,都需要延迟加载它。看来这是与angular-oauth2-oidc和Angular版本5有关的内部问题,这导致了循环依赖性。另一个选择是更新到Angular6。我已经尝试过了,在v6上没有这样的错误。

  constructor(private injector: Injector) { }

  private get oauthService() {
    return this.injector.get(OAuthService)
  }