我的情况是这样,我要动态加载组件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
请问到底怎么样才可以实现
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里面是一个比较简单的方法
同问,按照错误信息提示,说是需要把管道定义到更高级的module里面,然后尝试了放到:app.module里面也不行,引用管道的两个地方是平行级别的两个模块。