繁体   English   中英

Angular2 rc.6在组件中使用管道

[英]Angular2 rc.6 Use pipe in component

在rc.5中,我通常在组件中执行以下操作:

private myCustomPipe = new CustomPipe()
this.myCustomPipe.transform(...)

但是,由于我确实迁移了该应用程序,所以我在新的CustomPipe()上出错(我确实将注入从组件移动到模块的声明中)。

在app.module.ts中:

@NgModule({
    declarations: [CustomPipe],
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule
    ],
    bootstrap: [AppComponent],
})
export class AppModule {}

知道如何在Rc.6中实现吗?

如果要不仅在视图中在代码(组件,服务等)中使用它,还需要将其添加到提供程序中

@NgModule({
    declarations: [CustomPipe], // only for view
    providers: [CustomPipe], // to inject to components or services
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule
    ],
    bootstrap: [AppComponent],
})
export class AppModule {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM