Angular 2 - 循环依赖导致 'unexpected value undefined' 错误

Angular 2 - Circular dependencies causes 'unexpected value undefined' error

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

我的 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 中,它应该位于 !

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

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