Angular 生命週期鉤子是允許開發人員利用 Angular 元件生命週期的關鍵時刻的方法,從創建到銷毀,包括初始化、更改和銷毀。最常用的生命週期鉤子是:
- 建構子:頁面第一次載入時呼叫。只打過一次電話。
- ngOnChanges:執行多次。第一次將在元件建立/載入時執行。當 @input 裝飾器的自訂屬性發生變更時,每次都會呼叫此鉤子。與爭論一起工作 - 簡單的改變
- ngOnInit:組件初始化後呼叫。非常適合設定組件的狀態。
- ngDoCheck:用於手動偵測變更(在每個變更偵測週期呼叫)。
- ngAfterContentInit:內容投影到元件後呼叫。
- ngAfterContentChecked:檢查投影內容後呼叫。
- ngAfterViewInit:在視圖初始化後呼叫。
- ngAfterViewChecked:在 Angular 檢查元件視圖後呼叫。
- ngOnDestroy:在元件被銷毀之前呼叫。用它來清理資源,例如取消訂閱 observables。
在深入之前,讓我們先建立先決項目:
我們將需要父元件和子元件。我們將在父組件中有輸入字段,並將輸入的值傳遞給子組件,並將顯示在子組件中。
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 id="Lifecycle-Hooks">Lifecycle Hooks</h1> <input type="text" placeholder="Input here..."> <button>Submit Value</button> <br><br> <app-child></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.建構子
- 建構子是用來初始化元件的 TypeScript 類別方法。它在任何 Angular 生命週期鉤子之前調用。
- 主要用途:初始化依賴注入並設定變數。
export class ChildComponent implements OnInit { constructor() { **console.log("Constructor Called");** } @Input() inputValue: string = "LifeCycle Hooks"; ngOnInit(): void {} }
2.ngOnChanges
- 當元件的任何輸入屬性變更時呼叫。
- 提供一個 SimpleChanges 對象,其中包含輸入屬性的先前值和目前值。
- 用法:更新父元件的資料輸入屬性來觸發此鉤子。
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 id="Lifecycle-Hooks">Lifecycle Hooks</h1> <input type="text" placeholder="Input here..."> <button>Submit Value</button> <br><br> <app-child></app-child>
讓我們輸入一些數值來看:
3.ngOnInit
- 在第一個 ngOnChanges 之後呼叫一次。
- 主要用途:初始化元件並設定渲染所需的任何資料。
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
- 每次 Angular 偵測到元件或其子元件發生變更時執行。
- 將其用於自訂變更偵測邏輯。
<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
- 元件視圖及其子視圖初始化後呼叫一次。
- 對於初始化第三方函式庫或 DOM 操作很有用。
8.ngAfterViewChecked
- 每次檢查元件視圖及其子視圖後呼叫。
9.ngOnDestroy
- 在元件被銷毀之前呼叫。
- 將其用於清理任務,例如取消訂閱 Observables 或分離事件偵聽器。
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 id="Lifecycle-Hooks">Lifecycle Hooks</h1> <input type="text" placeholder="Input here..."> <button>Submit Value</button> <br><br> <app-child></app-child>
在我們點擊「銷毀組件」按鈕之前:
點選「銷毀組件」按鈕後:
生命週期掛鉤序列:
- 建構子
- ngOnChanges(如果 @Input 屬性存在)
- ngOnInit
- ngDoCheck
- ngAfterContentInit
- ngAfterContentChecked
- ngAfterViewInit
- ngAfterViewChecked
- ngOnDestroy
透過有效地理解和使用這些鉤子,您可以管理元件在其生命週期的不同階段的行為。
以上是Angular 中的組件生命週期的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr

Node.js擅長於高效I/O,這在很大程度上要歸功於流。 流媒體匯總處理數據,避免內存過載 - 大型文件,網絡任務和實時應用程序的理想。將流與打字稿的類型安全結合起來創建POWE

Python和JavaScript在性能和效率方面的差異主要體現在:1)Python作為解釋型語言,運行速度較慢,但開發效率高,適合快速原型開發;2)JavaScript在瀏覽器中受限於單線程,但在Node.js中可利用多線程和異步I/O提升性能,兩者在實際項目中各有優勢。

JavaScript起源於1995年,由布蘭登·艾克創造,實現語言為C語言。 1.C語言為JavaScript提供了高性能和系統級編程能力。 2.JavaScript的內存管理和性能優化依賴於C語言。 3.C語言的跨平台特性幫助JavaScript在不同操作系統上高效運行。

JavaScript在瀏覽器和Node.js環境中運行,依賴JavaScript引擎解析和執行代碼。 1)解析階段生成抽象語法樹(AST);2)編譯階段將AST轉換為字節碼或機器碼;3)執行階段執行編譯後的代碼。

Python和JavaScript的未來趨勢包括:1.Python將鞏固在科學計算和AI領域的地位,2.JavaScript將推動Web技術發展,3.跨平台開發將成為熱門,4.性能優化將是重點。兩者都將繼續在各自領域擴展應用場景,並在性能上有更多突破。

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3漢化版
中文版,非常好用

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

WebStorm Mac版
好用的JavaScript開發工具