成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來到九壹網(wǎng)。
搜索
您的當(dāng)前位置:首頁Angular2的管道Pipe的使用方法

Angular2的管道Pipe的使用方法

來源:九壹網(wǎng)

管道Pipe可以將數(shù)據(jù)作為輸入,然后按照規(guī)則將其轉(zhuǎn)換并輸出。在Angular2中有許多內(nèi)置的Pipe,比如DatePipe、UpperCasePipe和CurrencyPipe等。在這里我們主要介紹如何自定義Pipe。

1. 管道定義

Pipe的定義如下代碼所示:

import { PipeTransform, Pipe } from '@angular/core';

/*users: Array<any> = [
 { name: '1', id: 1 },
 { name: '2', id: 2 },
 { name: '3', id: 3 },
 { name: '4', id: 4 },
 { name: '5', id: 5 },
 { name: '6', id: 6 }
];*/

@Pipe({ name: 'filterUser' })
export class FilterUserPipe implements PipeTransform {
 transform(allUsers: Array<any>, ...args: any[]): any {
 return allUsers.filter(user => user.id > 3);
 }
}

如代碼所示,

  1. 需要使用@Pipe來裝飾類
  2. 實現(xiàn)PipeTransform的transform方法,該方法接受一個輸入值和一些可選參數(shù)
  3. 在@Pipe裝飾器中指定管道的名字,這個名字就可以在模板中使用。

注意:當(dāng)定義完成后,不要忘記在Module的declarations數(shù)組中包含這個管道。

2. 管道使用

user.template.html實現(xiàn)如下所示:

<div>
 <ul>
 <li *ngFor="let user of (users | filterUser)">
 {{user.name}}
 </li>
 </ul>
</div>
<button (click)="addUser()"> add new user</button>

user.component.ts實現(xiàn)如下所示:

import { Component } from "@angular/core";

@Component({
 templateUrl: './user.template.html',
})

export class EnvAppComponent {
 id = 7;
 users: Array<any> = [
 { name: '1', id: 1 },
 { name: '2', id: 2 },
 { name: '3', id: 3 },
 { name: '4', id: 4 },
 { name: '5', id: 5 },
 { name: '6', id: 6 }
 ];

 addUser() {
 this.users.push({ name: this.id++, id: this.id++ })
 }
}

在user.component.ts中初始了數(shù)據(jù)users和定義一個添加user的方法,在user.template.html中使用自定義管道filterUser。

當(dāng)啟動應(yīng)用時,可以發(fā)現(xiàn)只有id大于3的user被顯示出來了??墒牵?dāng)你點(diǎn)擊按鈕添加用戶時,發(fā)現(xiàn)并沒有什么反應(yīng),數(shù)據(jù)并沒有改變。這是為什么呢?

3. 數(shù)據(jù)變更檢測

在Angular2中管道分為兩種:純管道和非純管道。默認(rèn)情況下管道都是純管道。

純管道就是在Angular檢測到它的輸入值發(fā)生了純變更時才會執(zhí)行管道。純變更也就是說原始數(shù)據(jù)類型(如String、Number和Boolean等)或者對象的引用發(fā)生變化。該管道會忽略對象內(nèi)部的變化,在示例中,數(shù)組的引用沒有發(fā)生改變,改變的只是數(shù)組內(nèi)部的數(shù)據(jù),所以當(dāng)我們添加數(shù)據(jù)時并沒有立即響應(yīng)在頁面上。

非純管道會在組件的變更檢測周期中執(zhí)行,而且會對對象的內(nèi)部數(shù)據(jù)進(jìn)行檢測。

在我們的示例中將管道變更為非純管道是非常賤簡單的,只要在管道元數(shù)據(jù)中將添加pure標(biāo)志為false即可。

代碼如下所示:

@Pipe({ name: 'filterUser', pure: false })
export class FilterUserPipe implements PipeTransform {
 transform(allUsers: Array<any>, ...args: any[]): any {
 return allUsers.filter(user => user.id > 3);
 }
}

這樣每當(dāng)我們添加新用戶時,數(shù)據(jù)就會馬上響應(yīng)在頁面上了。

在根模塊聲明的pipe在頁面中引用有效,而在頁面中引用的component中的模板則無效,這也是令我困惑的地方...

尋求了一些解決方案,大多是要注意得在根模塊中聲明,于是我做了個嘗試,將組件也寫成一個功能模塊,并在組件功能模塊中申明pipe,結(jié)果很欣喜,組件中的pipe生效了。

具體操作方法:

只需新建組件功能模塊,并在改模塊中申明pipe,myComponent.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { myComponent } from 'myComponent.ts';
import { HelloPipe } from "hello.pipe.ts";

@NgModule({
 declarations: [
 myComponent,
 HelloPipe
 ],

 imports: [
 IonicPageModule.forChild(myComponent)
 ],

 exports: [
 myComponent
 ]
})

export class MyComponent {}

大功告成,組件的模板引用pipe得以生效.

Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號-2

違法及侵權(quán)請聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)