【发布时间】:2020-12-18 05:54:36
【问题描述】:
我想在我的所有组件中使用自定义管道,根据互联网,我应该使用一个共享模块,我在其中导入/导出管道。当我在要使用管道的组件中导入共享模块时,它应该可以工作,但不适用于我。
管道: 从 '@angular/core' 导入 { Pipe, PipeTransform };
@Pipe({
name: 'empty'
})
export class EmptyPipe implements PipeTransform {
public transform(value: any, ...args: any[]): string {
if (!value || value.length < 1) {
if (args.length < 1) {
return "-"
} else {
return args[0];
}
}
return value;
}
}
共享模块:
import { CommonModule } from '@angular/common';
import { NgModule} from '@angular/core';
import { EmptyPipe } from './pipes/empty.pipe';
@NgModule({
imports: [CommonModule],
declarations: [EmptyPipe],
exports: [EmptyPipe, CommonModule]
})
export class SharedModule {
}
我如何将它导入到我想使用它的组件中:
import { SharedModule } from '../shared/shared.module';
我如何尝试使用它:
{{companyList.earliest | empty}}
错误:
The pipe 'empty' could not be found
【问题讨论】:
-
也请分享你的模块,你可能忘记导入或导出管道
-
也请分享您的模块代码
-
您是否尝试将其添加为导入:[haredModule.forRoot()]