ng2学习--pipe使用

We use our custom pipe the same way we use built-in pipes.(自定义Pipe和API里自带的Pipe使用方式一致)

We must include our pipe in the declarations array of the AppModule.(Pipe要起作用的话,需要把它放到AppModule中declarations属性数组中。)

 

如果你正在按照《迈向Angular 2: 基于TypeScirpt的高性能SPA框架》学习的话,你会发现,Componet中没有pipes这个属性,而书中的例子就有!

我写文档这个时间(2017/3/1),从官方下载的quick-start里面,就不再支持这样的写法了,而正如我开头”红字“所写的那样,Pipe需要放在app.component.ts这个文件中的AppModule类的declarations属性中,

declarations这个属性的值是一个数组,我们先把我们自定义的或api中的pipe导入到这个文件中(例如:import { lowercasePipe } from './app.lowercase';),然后再把pipe添加到数组中。

 

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { lowercasePipe } from './app.lowercase';//这个是我自己写的pipe

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

 

posted @ 2017-03-01 19:25  lishidefengchen  阅读(771)  评论(0编辑  收藏  举报