首页 文章

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

提问于
浏览
1

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

我的 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的一些东西,所以我在_1602963中导入它,如下所示:

@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 .

But in this case the pipes aren't anymore in the SharedModule, where it should be !

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

1 回答

  • 1

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

相关问题