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中文网其他相关文章!