最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談angular4生命周期鉤子

 更新時間:2017年09月05日 14:19:39   作者:北城以南  
本篇文章主要介紹了淺談angularr4生命周期鉤子,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

理解

Angular提供了生命周期鉤子,把這些關(guān)鍵生命時刻暴露出來,賦予我們在它們發(fā)生時采取行動的能力??梢詫^子函數(shù)理解為在合適的時候做合適的事情。

鉤子函數(shù)

ng4主要提供了8個鉤子函數(shù):

1、ngOnchanges

@input屬性(輸入屬性)發(fā)生變化時,會調(diào)用。非此屬性,不會調(diào)用。

當(dāng)輸入屬性為對象時,當(dāng)對象的屬性值發(fā)生變化時,不會調(diào)用,當(dāng)對象的引用變化時會觸發(fā)。

先于ngOnInit調(diào)用。

2、ngOnInit

只執(zhí)行一次,dom操作可放在其中。(最常用)

3、NgDocheck

每次發(fā)生變更檢測時會被調(diào)用

ngDoCheck() 是Angular中的變更檢測機制.它由 zone.js 來實現(xiàn)的.其行為是只要你的Angular中的某個組件發(fā)生異步事件.就會檢查整個組件樹,以保證組件屬性的變化或頁面的變化是同步的.所以 ngDoCheck() 的觸發(fā)相當(dāng)頻繁的.并且是我們無法預(yù)料到的.也許我們在頁面上的一個無意識操作,就會觸發(fā)幾個甚至幾十個的 ngDoCheck() 生命周期鉤子.

4、ngAfterContentInit

在組件內(nèi)容初始化之后調(diào)用

5、ngAfterContentChecked

內(nèi)容投影:父組件寫在子標(biāo)簽之間的內(nèi)容會被渲染到子模板的ng-content中去,類似vue的slot

組件及子組件每次檢查內(nèi)容時調(diào)用

當(dāng)父子組件都有該鉤子時,父組件先執(zhí)行。

6、ngAfterViewInt

組件相應(yīng)的視圖初始化之后調(diào)用

7、ngAfterViewChecked

組件及子組件每次檢查視圖時調(diào)用

當(dāng)父子組件都有該鉤子時,子組件先執(zhí)行。

ngAfterViewChecked與ngAfterViewInt中不允許修改綁定的屬性(@input屬性),否則拋出異常

8、ngOnDestory

銷毀,事件解綁。

3.執(zhí)行順序

父組件:

組件模板

<div class="panel-body">
 <input type="text" [(ngModel)]="name">
 {{name}}
  <son [name]="name"></son>
</div>

組件

@Component({
 selector: 'father',
 templateUrl: './father.component.html',
 styleUrls: ['./father.component.scss']
})
export class FatherComponent implements OnInit {
 public name:string;
 constructor() { }
 ngOnInit() {
 console.log("父組件ngOninit");
 }
 ngOnchanges(){
 console.log("父組件ngonchanges");
 }
 ngDoCheck (){
 console.log("父組件ngDocheck")
 }
 ngAfterContentInit(){
 console.log("父組件ngAfterContentInit")
 }
 ngAfterContentChecked(){
 console.log("父組件ngAfterContentChecked")
 }
 ngAfterViewInit(){
 console.log("父組件ngAfterViewInit")
 }
 ngAfterViewChecked(){
 console.log("父組件ngAfterViewChecked")
 }
}

子組件

@Component({
 selector: 'son',
 templateUrl: './son.component.html',
 styleUrls: ['./son.component.scss']
})
export class SonComponent implements OnInit {
 @Input() name:string;
 constructor() { }

 ngOnInit() {
 console.log("子組件ngOninit");
 }
 ngOnChanges (){
 console.log("子組件ngonchanges");
 }
 ngDoCheck (){
 console.log("子組件ngDocheck")
 }
 ngAfterContentInit(){
 console.log("子組件ngAfterContentInit")
 }
 ngAfterContentChecked(){
 console.log("子組件ngAfterContentChecked")
 }
 ngAfterViewInit(){
 console.log("子組件ngAfterViewInit")
 }
 ngAfterViewChecked(){
 console.log("子組件ngAfterViewChecked")
 }
 
}

看打印結(jié)果:

當(dāng)在父組件的input中輸入內(nèi)容時,會打印如下結(jié)果:

看到有人說只有當(dāng)使用內(nèi)容投影時才會調(diào)用ngAfterConentChecked,當(dāng)上面的里面的代碼很顯然是沒用ng-content的,不知道該怎么解釋這個ngAfterConentChecked。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

开化县| 信阳市| 金门县| 阳东县| 大荔县| 高平市| 伽师县| 东安县| 南汇区| 通许县| 白山市| 文水县| 华安县| 马公市| 凤冈县| 灵台县| 丹江口市| 修水县| 垣曲县| 绵阳市| 腾冲县| 通江县| 浙江省| 康马县| 文成县| 溧阳市| 永嘉县| 尼玛县| 滨海县| 惠水县| 商水县| 留坝县| 鄂托克旗| 阳江市| 关岭| 吴堡县| 乐昌市| 秦皇岛市| SHOW| 突泉县| 滕州市|