亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁 web前端 js教程 Angular 中的組件生命週期

Angular 中的組件生命週期

Dec 26, 2024 am 05:02 AM

Angular 生命週期鉤子是允許開發(fā)人員利用 Angular 元件生命週期的關(guān)鍵時刻的方法,從創(chuàng)建到銷毀,包括初始化、更改和銷毀。最常用的生命週期鉤子是:

  1. 建構(gòu)子:頁面第一次載入時呼叫。只打過一次電話。
  2. ngOnChanges:執(zhí)行多次。第一次將在元件建立/載入時執(zhí)行。當(dāng) @input 裝飾器的自訂屬性發(fā)生變更時,每次都會呼叫此鉤子。與爭論一起工作 - 簡單的改變
  3. ngOnInit:組件初始化後呼叫。非常適合設(shè)定組件的狀態(tài)。
  4. ngDoCheck:用於手動偵測變更(在每個變更偵測週期呼叫)。
  5. ngAfterContentInit:內(nèi)容投影到元件後呼叫。
  6. ngAfterContentChecked:檢查投影內(nèi)容後呼叫。
  7. ngAfterViewInit:在視圖初始化後呼叫。
  8. ngAfterViewChecked:在 Angular 檢查元件視圖後呼叫。
  9. ngOnDestroy:在元件被銷毀之前呼叫。用它來清理資源,例如取消訂閱 observables。

Component Lifecycle in 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>

我們將得到以下輸出:

Component Lifecycle in Angular

1.建構(gòu)子

  • 建構(gòu)子是用來初始化元件的 TypeScript 類別方法。它在任何 Angular 生命週期鉤子之前調(diào)用。
  • 主要用途:初始化依賴注入並設(shè)定變數(shù)。
export class ChildComponent implements OnInit {

  constructor() {
    **console.log("Constructor Called");**
  }

  @Input() inputValue: string = "LifeCycle Hooks";

  ngOnInit(): void {}

}

Component Lifecycle in Angular

2.ngOnChanges

  • 當(dāng)元件的任何輸入屬性變更時呼叫。
  • 提供一個 SimpleChanges 對象,其中包含輸入屬性的先前值和目前值。
  • 用法:更新父元件的資料輸入屬性來觸發(fā)此鉤子。
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;
  }

}

Component Lifecycle in Angular

我再次輸入了值,並再次呼叫了 ngOnChanges,但建構(gòu)子只呼叫了一次。

Component Lifecycle in Angular

讓我們看看更改參數(shù)中有什麼:

<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>

Component Lifecycle in Angular

讓我們輸入一些數(shù)值來看:

Component Lifecycle in Angular

3.ngOnInit

  • 在第一個 ngOnChanges 之後呼叫一次。
  • 主要用途:初始化元件並設(shè)定渲染所需的任何資料。
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 {
  }

}

Component Lifecycle in Angular

4.ngDoCheck

  • 每次 Angular 偵測到元件或其子元件發(fā)生變更時執(zhí)行。
  • 將其用於自訂變更偵測邏輯。
<div>
    Input Value: <strong>{{inputValue}}</strong>
</div>

Component Lifecycle in Angular

5.ngAfterContentInit

  • 內(nèi)容(例如,)投影到元件後呼叫一次。

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);
  }

Component Lifecycle in Angular

6.ngAfterContentChecked

  • 每次檢查投影內(nèi)容後呼叫。
  • 謹(jǐn)慎使用以避免效能問題。
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");
  }

}

Component Lifecycle in Angular

讓我們來玩一下這個:

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");
  }

}

當(dāng) ng-content 再次改變時,ngAfterContentChecked 被呼叫。

Component Lifecycle in Angular

7.ngAfterViewInit

  • 元件視圖及其子視圖初始化後呼叫一次。
  • 對於初始化第三方函式庫或 DOM 操作很有用。

Component Lifecycle in Angular

8.ngAfterViewChecked

  • 每次檢查元件視圖及其子視圖後呼叫。

Component Lifecycle in Angular

9.ngOnDestroy

  • 在元件被銷毀之前呼叫。
  • 將其用於清理任務(wù),例如取消訂閱 Observables 或分離事件偵聽器。

ngOnDestroy 僅在我們銷毀任何組件時才會調(diào)用,因此讓我們嘗試在單擊“銷毀組件”按鈕時刪除子組件。
讓我們來安排一下:

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>

在我們點擊「銷毀組件」按鈕之前:

Component Lifecycle in Angular

點選「銷毀組件」按鈕後:

Component Lifecycle in Angular

生命週期掛鉤序列:

  1. 建構(gòu)子
  2. ngOnChanges(如果 @Input 屬性存在)
  3. ngOnInit
  4. ngDoCheck
  5. ngAfterContentInit
  6. ngAfterContentChecked
  7. ngAfterViewInit
  8. ngAfterViewChecked
  9. ngOnDestroy

透過有效地理解和使用這些鉤子,您可以管理元件在其生命週期的不同階段的行為。

以上是Angular 中的組件生命週期的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

垃圾收集如何在JavaScript中起作用? 垃圾收集如何在JavaScript中起作用? Jul 04, 2025 am 12:42 AM

JavaScript的垃圾回收機制通過標(biāo)記-清除算法自動管理內(nèi)存,以減少內(nèi)存洩漏風(fēng)險。引擎從根對像出發(fā)遍歷並標(biāo)記活躍對象,未被標(biāo)記的則被視為垃圾並被清除。例如,當(dāng)對像不再被引用(如將變量設(shè)為null),它將在下一輪迴收中被釋放。常見的內(nèi)存洩漏原因包括:①未清除的定時器或事件監(jiān)聽器;②閉包中對外部變量的引用;③全局變量持續(xù)持有大量數(shù)據(jù)。 V8引擎通過分代回收、增量標(biāo)記、並行/並發(fā)回收等策略優(yōu)化回收效率,降低主線程阻塞時間。開發(fā)時應(yīng)避免不必要的全局引用、及時解除對象關(guān)聯(lián),以提升性能與穩(wěn)定性。

如何在node.js中提出HTTP請求? 如何在node.js中提出HTTP請求? Jul 13, 2025 am 02:18 AM

在Node.js中發(fā)起HTTP請求有三種常用方式:使用內(nèi)置模塊、axios和node-fetch。 1.使用內(nèi)置的http/https模塊無需依賴,適合基礎(chǔ)場景,但需手動處理數(shù)據(jù)拼接和錯誤監(jiān)聽,例如用https.get()獲取數(shù)據(jù)或通過.write()發(fā)送POST請求;2.axios是基於Promise的第三方庫,語法簡潔且功能強大,支持async/await、自動JSON轉(zhuǎn)換、攔截器等,推薦用於簡化異步請求操作;3.node-fetch提供類似瀏覽器fetch的風(fēng)格,基於Promise且語法簡單

JavaScript數(shù)據(jù)類型:原始與參考 JavaScript數(shù)據(jù)類型:原始與參考 Jul 13, 2025 am 02:43 AM

JavaScript的數(shù)據(jù)類型分為原始類型和引用類型。原始類型包括string、number、boolean、null、undefined和symbol,其值不可變且賦值時復(fù)制副本,因此互不影響;引用類型如對象、數(shù)組和函數(shù)存儲的是內(nèi)存地址,指向同一對象的變量會相互影響。判斷類型可用typeof和instanceof,但需注意typeofnull的歷史問題。理解這兩類差異有助於編寫更穩(wěn)定可靠的代碼。

JavaScript時間對象,某人構(gòu)建了一個eactexe,在Google Chrome上更快的網(wǎng)站等等 JavaScript時間對象,某人構(gòu)建了一個eactexe,在Google Chrome上更快的網(wǎng)站等等 Jul 08, 2025 pm 02:27 PM

JavaScript開發(fā)者們,大家好!歡迎閱讀本週的JavaScript新聞!本週我們將重點關(guān)注:Oracle與Deno的商標(biāo)糾紛、新的JavaScript時間對象獲得瀏覽器支持、GoogleChrome的更新以及一些強大的開發(fā)者工具。讓我們開始吧! Oracle與Deno的商標(biāo)之爭Oracle試圖註冊“JavaScript”商標(biāo)的舉動引發(fā)爭議。 Node.js和Deno的創(chuàng)建者RyanDahl已提交請願書,要求取消該商標(biāo),他認(rèn)為JavaScript是一個開放標(biāo)準(zhǔn),不應(yīng)由Oracle

React與Angular vs Vue:哪個JS框架最好? React與Angular vs Vue:哪個JS框架最好? Jul 05, 2025 am 02:24 AM

選哪個JavaScript框架最好?答案是根據(jù)需求選擇最適合的。 1.React靈活自由,適合需要高度定制、團隊有架構(gòu)能力的中大型項目;2.Angular提供完整解決方案,適合企業(yè)級應(yīng)用和長期維護的大項目;3.Vue上手簡單,適合中小型項目或快速開發(fā)。此外,是否已有技術(shù)棧、團隊規(guī)模、項目生命週期及是否需要SSR也都是選擇框架的重要因素??傊瑳]有絕對最好的框架,適合自己需求的就是最佳選擇。

立即在JavaScript中立即調(diào)用功能表達(dá)式(IIFE) 立即在JavaScript中立即調(diào)用功能表達(dá)式(IIFE) Jul 04, 2025 am 02:42 AM

IIFE(ImmediatelyInvokedFunctionExpression)是一種在定義後立即執(zhí)行的函數(shù)表達(dá)式,用於變量隔離和避免污染全局作用域。它通過將函數(shù)包裹在括號中使其成為表達(dá)式,並緊隨其後的一對括號來調(diào)用,如(function(){/code/})();。其核心用途包括:1.避免變量衝突,防止多個腳本間的命名重複;2.創(chuàng)建私有作用域,使函數(shù)內(nèi)部變量不可見;3.模塊化代碼,便於初始化工作而不暴露過多變量。常見寫法包括帶參數(shù)傳遞的版本和ES6箭頭函數(shù)版本,但需注意:必須使用表達(dá)式、結(jié)

處理諾言:鏈接,錯誤處理和承諾在JavaScript中 處理諾言:鏈接,錯誤處理和承諾在JavaScript中 Jul 08, 2025 am 02:40 AM

Promise是JavaScript中處理異步操作的核心機制,理解鍊式調(diào)用、錯誤處理和組合器是掌握其應(yīng)用的關(guān)鍵。 1.鍊式調(diào)用通過.then()返回新Promise實現(xiàn)異步流程串聯(lián),每個.then()接收上一步結(jié)果並可返回值或Promise;2.錯誤處理應(yīng)統(tǒng)一使用.catch()捕獲異常,避免靜默失敗,並可在catch中返回默認(rèn)值繼續(xù)流程;3.組合器如Promise.all()(全成功才成功)、Promise.race()(首個完成即返回)和Promise.allSettled()(等待所有完成)

什麼是緩存API?如何與服務(wù)人員使用? 什麼是緩存API?如何與服務(wù)人員使用? Jul 08, 2025 am 02:43 AM

CacheAPI是瀏覽器提供的一種緩存網(wǎng)絡(luò)請求的工具,常與ServiceWorker配合使用,以提升網(wǎng)站性能和離線體驗。 1.它允許開發(fā)者手動存儲如腳本、樣式表、圖片等資源;2.可根據(jù)請求匹配緩存響應(yīng);3.支持刪除特定緩存或清空整個緩存;4.通過ServiceWorker監(jiān)聽fetch事件實現(xiàn)緩存優(yōu)先或網(wǎng)絡(luò)優(yōu)先等策略;5.常用於離線支持、加快重複訪問速度、預(yù)加載關(guān)鍵資源及後臺更新內(nèi)容;6.使用時需注意緩存版本控制、存儲限制及與HTTP緩存機制的區(qū)別。

See all articles