代码之家  ›  专栏  ›  技术社区  ›  Pritam Bohra

在角度7中检测到循环依赖性

  •  2
  • Pritam Bohra  · 技术社区  · 6 年前

    我的Angular7应用程序中有以下应用程序结构:

    AppModule
    DashboardModule
      DashboardChild1
      DashboardChild2
      DashboardChild3
      DashboardService
    AdminModule
      AdminChild1
      AdminChild2
      AdminChild3
    

    我希望仪表盘服务仅在 DashboardModule ,所以我跟踪了这个链接 providedin-and-ngmodules .

    这是我的 DashboardService :

    import { Injectable } from '@angular/core';
    import { DashboardModule } from './dashboard.module';
    
    @Injectable({
      providedIn: DashboardModule
    })
    export class DashboardService {
      .......
    }
    

    我在DashboardChild1组件中使用了该服务,但它给出了以下错误:

    检测到循环依赖项中的警告: src/app/dashboard/dashboard-child1/dashboard-child1.component.ts-> src/app/dashboard/dashboard.service.ts-> src/app/dashboard/dashboard.module.ts-> src/app/dashboard/dashboard-routing.module.ts-> src/app/dashboard/dashboard-child1/dashboard-child1.component.ts

    检测到循环依赖项中的警告: src/app/dashboard/dashboard-routing.module.ts-> src/app/dashboard/dashboard-child1/dashboard-child1.component.ts-> src/app/dashboard/dashboard.service.ts-> src/app/dashboard/dashboard.module.ts-> src/app/dashboard/dashboard-routing.module.ts网站

    检测到循环依赖项中的警告: src/app/dashboard/dashboard.module.ts-> src/app/dashboard/dashboard-routing.module.ts-> src/app/dashboard/dashboard-child1/dashboard-child1.component.ts-> src/app/dashboard/dashboard.service.ts-> src/app/dashboard/dashboard.module.ts网站

    检测到循环依赖项中的警告: src/app/dashboard/dashboard.service.ts-> src/app/dashboard/dashboard.module.ts-> src/app/dashboard/dashboard-routing.module.ts-> src/app/dashboard/dashboard-child1/dashboard-child1.component.ts-> src/app/dashboard/dashboard.service.ts网站

    我这里缺什么?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Derviş Kayımbaşıoğlu    6 年前

    这最有可能发生在

    DashboardChild1是DashboardModule的成员,DashboardChild1尝试通过注入访问DashboardModule。其结果是循环依赖。

    DashboardModule calls DashboardChild1 
    DashboardChild1 calls DashboardModule
    DashboardModule class DashboardChild1
    DashboardChild1 calls DashboardModule
    ...
    ...
    ...
    ...
    

    同样适用于DashboardService

    DashboardModule calls DashboardChild1 calls DashboardService
    DashboardService calls DashboardModule
    DashboardModule calls DashboardChild1 calls DashboardService
    DashboardService calls DashboardModule
    ...
    ...
    ...
    ...