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

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条)

报告相同问题?