一丝混乱 2019-01-14 12:12 采纳率: 50%
浏览 1161
已采纳

angular中,多个组件引用同一个管道

我的情况是这样,我要动态加载组件C1,然后C1里有子组件C2、C3。而C2、C3同时使用了管道P1。

当C2和C3同时declarations: [XXComponent, P1Pipe]时报错:

Type P1Pipe is part of the declarations of 2 modules: C2Module and C3Module! Please consider moving P1Pipe to a higher module that imports C2Module and C3Module. You can also create a new NgModule that exports and includes P1Pipe then import that NgModule C2Module and C3Module

而把P1Pipe移到C1的declarations中之后又报错:
The pipe 'p1' could not be found

请问到底怎么样才可以实现

  • 写回答

2条回答 默认 最新

  • 一丝混乱 2019-08-01 20:19
    关注
    import { NgModule } from '@angular/core';
    import { ColumnTypePipe } from './is-column-type.pipe';
    import { PortTypePipe } from './is-port-type.pipe';
    
    @NgModule({
        imports:        [],
        declarations:   [ColumnTypePipe, PortTypePipe],
        exports:        [ColumnTypePipe, PortTypePipe],
    })
    
    export class PipeModule {
    }
    
    

    给管道单独定义一个module即可
    我这里同一个module定义了2个管道。

    也就是,除非某个管道只出现在一个组件里面,这样可以和组件放在一个module中,不然把所有的管道都定义在一个单独的module里面是一个比较简单的方法

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站