Angular中的生命周期實(shí)例分析

今天小編給大家分享一下Angular中的生命周期實(shí)例分析的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

遂寧網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,遂寧網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為遂寧近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的遂寧做網(wǎng)站的公司定做!

Angular中的生命周期實(shí)例分析

接觸過 reactvue 開發(fā)的讀者應(yīng)該對生命周期這個概念不陌生。我們在使用 angular 開發(fā)的過程中,是避免不了的。

組件從開始建立到銷毀的過程中,會經(jīng)歷過一系列的階段。這就是一個生命周期,這些階段對應(yīng)著應(yīng)用提供的 lifecycle hooks。

那么,在 angular 中,這些 hooks 都有哪些呢?了解它們,對你編寫程序應(yīng)該在哪里編寫,很重要。

angular 中,生命周期執(zhí)行的順序如下:

- constructor 【常用,不算鉤子函數(shù),但是很重要】
- ngOnChanges【常用】
- ngOnInit【常用】
- ngDoCheck
  - ngAfterContentInit
  - ngAfterContentChecked
  - ngAfterViewInit【常用】
  - ngAfterViewChecked
- ngOnDestroy【常用】

為了解說和驗(yàn)證,我們用 angular-cli 生成一個 demo 項(xiàng)目。

constructor

es6 中的 class 初始化對象的時(shí)候,constructor 會立即被調(diào)用。

class Person {
  constructor(name) {
    console.log('be called')
    this.name = name;
  }
}

let jimmy = new Person('jimmy'); // be called

angular 的組件本身就是導(dǎo)出一個類。當(dāng)這個組件被 new 起來的時(shí)候,會獲取 constructor 中的預(yù)設(shè)的值。

ngOnChanges

當(dāng)我們有外部參數(shù)更改的時(shí)候,我們就會執(zhí)行 ngOnChanges,也就是說組件中有 @Input 所綁定的屬性值發(fā)生改變的時(shí)候調(diào)用。

簡單說,父組件綁定子組件中的元素,會觸發(fā)這個鉤子函數(shù),可以多次出發(fā)。這在下面的 ngOnInit 總會介紹。

ngOnInit

這個方法調(diào)用的時(shí)候,說明組件已經(jīng)初始化成功。在第一次 ngOnChanges() 完成之后調(diào)用,且只調(diào)用一次。

// app.component.ts
export class AppComponent implements OnInit, OnChanges {

  constructor() {
    console.log('1. constructor')
  }

  ngOnChanges() {
    console.log('2. ngOnChanges')
  }

  ngOnInit() {
    console.log('3. ngOnInit')
  }
}

打印的信息如下:

Angular中的生命周期實(shí)例分析

咦?怎么沒有打印 ngOnChanges 中的鉤子函數(shù)信息呢?

上面已經(jīng)說過了,需要觸發(fā)條件 @Input 的屬性值改變的時(shí)候。我們來修改一下:

<!-- app.component.html -->
<div>
  <app-demo></app-demo>
</div>
// app.component.ts
// AppComponent 類中添加屬性
public count:number = 0;
<!-- demo.component.html -->
<h4>count: {{ count }}</h4>
// demo.component.ts
export class DemoComponent implements OnInit, OnChanges {

  @Input()
  public count: number;

  constructor() {
    console.log('1. demo constructor')
  }

  ngOnChanges() {
    console.log('2. demo ngOnChanges')
  }

  ngOnInit() {
    console.log('3. demo ngOnInit')
  }

}

Angular中的生命周期實(shí)例分析

當(dāng)通過 @Input 將值傳遞給子組件 demo 的時(shí)候,就會觸發(fā) demo 組件中的 ngOnChanges。

當(dāng) @Input 傳遞的屬性發(fā)生改變的時(shí)候,可以多次觸發(fā) demo 組件中的 ngOnChanges 鉤子函數(shù)。

<!-- app.component.html -->
<div>
  <app-demo [count]="count"></app-demo>

  <button (click)="parentDemo()">parent button</button>
</div>
// app.component.ts
parentDemo() {
  this.count++;
}

ngDoCheck

當(dāng)發(fā)生變化檢測的時(shí)候,觸發(fā)該鉤子函數(shù)。

這個鉤子函數(shù),緊跟在每次執(zhí)行變更檢測時(shí)候 ngOnChanges 和首次執(zhí)行執(zhí)行變更檢測時(shí) ngOnInit 后面調(diào)用。

// demo.component.ts

ngDoCheck() {
  console.log('4. demo ngDoCheck')
}

這個鉤子函數(shù)調(diào)用得比較頻繁,使用成本比較高,謹(jǐn)慎使用。

一般使用 ngOnChanges 來檢測變動,而不是 ngDoCheck

ngAfterContentInit

當(dāng)把外部的內(nèi)容投影到內(nèi)部組件,第一次調(diào)用 ngDoCheck 之后調(diào)用 ngAfterContentInit,而且只調(diào)用一次。

// demo.component.ts

ngAfterContentInit() {
  console.log('5. demo ngAfterContentInit');
}

Angular中的生命周期實(shí)例分析

ngAfterContentChecked

ngAfterContentChecked 鉤子函數(shù)在每次 ngDoCheck 之后調(diào)用.

// demo.component.ts

ngAfterContentChecked() {
  console.log('5. demo ngAfterContentChecked');
}

ngAfterViewInit

視圖初始化完成調(diào)用此鉤子函數(shù)。在第一次 ngAfterContentChecked 之后調(diào)用,只調(diào)用一次。

這個時(shí)候,獲取頁面的 DOM 節(jié)點(diǎn)比較合理

// demo.compoent.ts

ngAfterViewInit() {
  console.log('7. demo ngAfterViewInit');
}

Angular中的生命周期實(shí)例分析

ngAfterViewChecked

視圖檢測完成調(diào)用。在 ngAfterViewinit 后調(diào)用,和在每次 ngAfterContentChecked 之后調(diào)用,也就是在每次 ngDoCheck 之后調(diào)用。

// demo.component.ts

ngAfterViewChecked() {
  console.log('8. ngAfterViewChecked')
}

ngOnDestroy

組件被銷毀時(shí)候進(jìn)行的操作。

在這個鉤子函數(shù)中,我們可以取消訂閱,取消定時(shí)操作等等。

<!-- app.component.html -->
<app-demo [count]="count" *ngIf="showDemoComponent"></app-demo>

<button (click)="hideDemo()">hide demo component</button>
// app.component.ts
public showDemoComponent: boolean = true;

hideDemo() {
  this.showDemoComponent = false
}
// demo.component.ts
ngOnDestroy() {
  console.log('9. demo ngOnDestroy')
}

以上就是“Angular中的生命周期實(shí)例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:Angular中的生命周期實(shí)例分析
當(dāng)前鏈接:http://bm7419.com/article24/gosjje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄移動網(wǎng)站建設(shè)、網(wǎng)站策劃外貿(mào)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管