Angular 2 - 循环依赖导致意外值未定义'错误

时间:2017-02-11 14:44:31

标签: angularjs dependency-injection module circular-dependency

我目前正在使用共享功能和核心模块清理我的应用。

我的 SharedModule 如下所示:

@NgModule({
  declarations: [
    TendanceNotePipe, 
    ColorNotePipe,
  ],
  exports: [
    CommonModule,
    FormsModule,
    RouterModule,

    SearchBoxModule, // Need the FormsModule and the pipes TendanceNotePipe, ColorNotePipe

    TendanceNotePipe, 
    ColorNotePipe
  ]
})
export class SharedModule {}

这里的问题是SearchBoxModule需要来自sharedModule的一些东西,所以我在 SearchBoxModule 中导入它,如下所示:

@NgModule({
  imports: [
    SharedModule, // Contains Pipes and FormsModule needed by the component of this module
    HttpModule
  ],
  declarations: [
    SearchBoxComponent,
    ResultsBoxComponent,
    ResultsListComponent
  ],
  exports: [SearchBoxComponent]
})
export class SearchBoxModule { }

我有错误:Error: Unexpected value 'undefined' imported by the module SharedModule。 我认为这是由于循环依赖?

SearchBoxModule是一个可重复使用的模块,其中组件在应用程序中被多次调用,有时在同一视图中两次,它的位置在ShareModule中吗?

我尝试在SearchBoxModule中手动导入依赖项(Pipes和FormsModule),但我还有另一个错误:Type TendanceNotePipe is part of the declarations of 2 modules: SharedModule and SearchBoxModule

我找到的唯一方法是将管道声明从SharedModule移到SearchBoxModule,然后导入FormsModule并从其导入列表中删除SharedModule。

但在这种情况下,管道已不再存在于SharedModule中,应该在哪里!

在这种情况下我应该怎么做?

1 个答案:

答案 0 :(得分:1)

您必须创建两个单独的模块。或者,您可以创建一个共享模块,并在SharedModule中导入SearchBoxComponent的所有依赖项,并添加到声明数组,然后从共享模块中导出搜索框组件。删除搜索框模块。