【问题标题】:Issue with same Pipe component declarations in child modules子模块中相同的管道组件声明问题
【发布时间】:2017-02-08 02:07:32
【问题描述】:

我想在父模块中声明管道并在子模块中使用它。

@NgModule({
  // Pipe which I want to declare in all child modules
  declarations: [ ThisIsPipe ],
  imports: [ ChildModuleOne, ChildModuleTwo],  
})

如何使用 It 子模块?

因为如果我声明两次,我就会出错

未捕获的错误:类型 ThisIsPipe 是 2 个模块声明的一部分:ChildModuleOne 和 ChildModuleTwo! 请考虑将 ThisIsPipe 移至导入 ChildModuleOne 和 ChildModuleTwo 的更高模块。 你也可以创建一个新的 NgModule 来导出和包含 ThisIsPipe 然后在 ChildModuleOne 和 ChildModuleTwo 中导入该 NgModule。

【问题讨论】:

    标签: angular typescript import


    【解决方案1】:

    作为Günter Zöchbauer 的回答, 在此处查找示例

    Angular 2 - Pipe reuse in multiple modules - error not found or duplicate definition

    【讨论】:

      【解决方案2】:

      您需要在放置管道的位置创建另一个模块,然后将该模块导入您要使用该管道的位置。

      一个指令、组件或管道总是只能属于一个确切的 NgModule,但是这个 NgModule 可以根据需要导入到任意数量的模块中。

      【讨论】:

      • 是否可以使用子模块中的父声明?
      • 不,您必须直接导入所有要使用声明的模块。你可以将几个可重用的组件、指令和管道放在一个 NgModule 中。您还可以创建一个 NgModule (A) 来导出其他 NgModule(B、C、D),以便您只需将 A 添加到当前模块的导入中即可导入 B、C、D。
      猜你喜欢
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 2019-06-01
      • 2018-01-28
      相关资源
      最近更新 更多