Angular 生命週期鉤子是允許開發人員利用 Angular 元件生命週期的關鍵時刻的方法,從創建到銷毀,包括初始化、更改和銷毀。最常用的生命週期鉤子是:
在深入之前,讓我們先建立先決項目:
我們將需要父元件和子元件。我們將在父組件中有輸入字段,並將輸入的值傳遞給子組件,並將顯示在子組件中。
parent.component.ts
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-parent', templateUrl: './parent.component.html', styleUrls: ['./parent.component.css'] }) export class ParentComponent implements OnInit { constructor() { } ngOnInit(): void { } value:string = ''; SubmitValue(val: any) { this.value = val.value; } }
parent.component.html
<h1>Lifecycle Hooks</h1> <input type="text" placeholder="Input here..." #val> <button (click)="SubmitValue(val)">Submit Value</button> <br><br> <app-child [inputValue]="value"></app-child>
child.component.ts
import { Component, Input, OnInit } from '@angular/core'; @Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.css'] }) export class ChildComponent implements OnInit { constructor() { } @Input() inputValue: string = "LifeCycle Hooks"; ngOnInit(): void { } }
child.component.html
<div> Input Value: <strong>{{inputValue}}</strong> </div>
我們將得到以下輸出:
1.建構子
export class ChildComponent implements OnInit { constructor() { **console.log("Constructor Called");** } @Input() inputValue: string = "LifeCycle Hooks"; ngOnInit(): void {} }
2.ngOnChanges
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-parent', templateUrl: './parent.component.html', styleUrls: ['./parent.component.css'] }) export class ParentComponent implements OnInit { constructor() { } ngOnInit(): void { } value:string = ''; SubmitValue(val: any) { this.value = val.value; } }
我再次輸入了值,並再次呼叫了 ngOnChanges,但建構子只呼叫了一次。
讓我們看看更改參數中有什麼:
<h1>Lifecycle Hooks</h1> <input type="text" placeholder="Input here..." #val> <button (click)="SubmitValue(val)">Submit Value</button> <br><br> <app-child [inputValue]="value"></app-child>
讓我們輸入一些數值來看:
3.ngOnInit
import { Component, Input, OnInit } from '@angular/core'; @Component({ selector: 'app-child', templateUrl: './child.component.html', styleUrls: ['./child.component.css'] }) export class ChildComponent implements OnInit { constructor() { } @Input() inputValue: string = "LifeCycle Hooks"; ngOnInit(): void { } }
4.ngDoCheck
<div> Input Value: <strong>{{inputValue}}</strong> </div>
5.ngAfterContentInit
child.component.html
export class ChildComponent implements OnInit { constructor() { **console.log("Constructor Called");** } @Input() inputValue: string = "LifeCycle Hooks"; ngOnInit(): void {} }
parent.component.html
export class ChildComponent implements OnInit, OnChanges { constructor() { console.log("Constructor Called"); } ngOnChanges(changes: SimpleChanges): void { console.log("ngOnChanges Called"); } @Input() inputValue: string = "LifeCycle Hooks"; ngOnInit(): void {} }
child.component.ts
ngOnChanges(changes: SimpleChanges): void { console.log("ngOnChanges Called", changes); }
6.ngAfterContentChecked
export class ChildComponent implements OnInit, OnChanges { constructor() { console.log("Constructor Called"); } ngOnChanges(changes: SimpleChanges): void { console.log("ngOnChanges Called"); } @Input() inputValue: string = "LifeCycle Hooks"; ngOnInit(): void { console.log("ngOnInit Called"); } }
讓我們來玩一下這個:
export class ChildComponent implements OnInit, OnChanges, DoCheck { constructor() { console.log("Constructor Called"); } ngOnChanges(changes: SimpleChanges): void { console.log("ngOnChanges Called", changes); } @Input() inputValue: string = "LifeCycle Hooks"; ngOnInit(): void { console.log("ngOnInit Called"); } ngDoCheck() { console.log("ngDoCheck Called"); } }
當 ng-content 再次改變時,ngAfterContentChecked 被呼叫。
7.ngAfterViewInit
8.ngAfterViewChecked
9.ngOnDestroy
ngOnDestroy 僅在我們銷毀任何組件時才會調用,因此讓我們嘗試在單擊“銷毀組件”按鈕時刪除子組件。
讓我們來安排一下:
parent.component.ts
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-parent', templateUrl: './parent.component.html', styleUrls: ['./parent.component.css'] }) export class ParentComponent implements OnInit { constructor() { } ngOnInit(): void { } value:string = ''; SubmitValue(val: any) { this.value = val.value; } }
parent.component.html
<h1>Lifecycle Hooks</h1> <input type="text" placeholder="Input here..." #val> <button (click)="SubmitValue(val)">Submit Value</button> <br><br> <app-child [inputValue]="value"></app-child>
在我們點擊「銷毀組件」按鈕之前:
點選「銷毀組件」按鈕後:
生命週期掛鉤序列:
透過有效地理解和使用這些鉤子,您可以管理元件在其生命週期的不同階段的行為。
以上是Angular 中的組件生命週期的詳細內容。更多資訊請關注PHP中文網其他相關文章!