Angular学习笔记54:Angular的生命周期钩子调用顺序探究

在Angular中,每一个组件都是通过Angular的管理着其自身的生命周期。Angular创建组件,渲染组件,如果当前组件有子组件,还会创建、渲染其子组件。在组件的属性发生变化的时候,进行检查,并在组件从DOM移除前销毁组件。而生命周期钩子就是在这些生命周期过程中,Angular将这些生命周期的时刻暴露出来,然后在相应的时刻,开发人员就可以采取相应的措施。

探索生命周期的钩子

  1. 新建一个组件
wujiayudeMacBook-Pro:pages wjy$ ng g c lifecycle-hooks
CREATE src/app/pages/lifecycle-hooks/lifecycle-hooks.component.less (0 bytes)
CREATE src/app/pages/lifecycle-hooks/lifecycle-hooks.component.html (34 bytes)
CREATE src/app/pages/lifecycle-hooks/lifecycle-hooks.component.spec.ts (685 bytes)
CREATE src/app/pages/lifecycle-hooks/lifecycle-hooks.component.ts (305 bytes)
UPDATE src/app/pages/pages.module.ts (2049 bytes)

  1. 引用各种各样的生命周期钩子,并做好输出标记
import {
  AfterContentChecked,
  AfterContentInit,
  AfterViewChecked,
  AfterViewInit,
  Component,
  DoCheck,
  OnChanges, OnDestroy,
  OnInit,
  SimpleChanges
} from '@angular/core';

@Component({
  selector: 'app-lifecycle-hooks',
  templateUrl: './lifecycle-hooks.component.html',
  styleUrls: ['./lifecycle-hooks.component.less']
})
export class LifecycleHooksComponent implements OnChanges, OnInit,
  DoCheck, AfterContentInit, AfterContentChecked, AfterViewInit, AfterViewChecked, OnDestroy {

  constructor() {
  }

  ngOnChanges(changes: SimpleChanges): void {
    console.log('this is ngOnChanges');
  }

  ngOnInit() {
    console.log('this is ngOnInit');
  }

  ngDoCheck(): void {
    console.log('this is ngDoCheck');
  }

  ngAfterContentInit(): void {
    console.log('this is ngAfterContentInit');
  }

  ngAfterContentChecked(): void {
    console.log('this is ngAfterContentChecked');
  }

  ngAfterViewInit(): void {
    console.log('this is ngAfterViewInit');
  }

  ngAfterViewChecked(): void {
    console.log('this is ngAfterViewChecked');
  }

  ngOnDestroy(): void {
    console.log('this is ngOnDestroy');
  }

}

保存运行以后,看控制台输出:

在这里插入图片描述

此时会发现钩子:ngOnChanges()并没有执行,那是因为当前的组件并没有绑定任何属性。

绑定一个属性,探索ngOnChanges()

  1. 修改类文件
import {
  AfterContentChecked,
  AfterContentInit,
  AfterViewChecked,
  AfterViewInit,
  Component,
  DoCheck, Input,
  OnChanges, OnDestroy,
  OnInit,
  SimpleChanges
} from '@angular/core';

@Component({
  selector: 'app-lifecycle-hooks',
  templateUrl: './lifecycle-hooks.component.html',
  styleUrls: ['./lifecycle-hooks.component.less']
})
export class LifecycleHooksComponent implements OnChanges, OnInit,
  DoCheck, AfterContentInit, AfterContentChecked, AfterViewInit, AfterViewChecked, OnDestroy {
  @Input() name: string;

  constructor() {
  }

  ngOnChanges(changes: SimpleChanges): void {
    console.log(changes);
    console.log('this is ngOnChanges');
  }

  ngOnInit() {
    console.log('this is ngOnInit');
  }

  ngDoCheck(): void {
    console.log('this is ngDoCheck');
  }

  ngAfterContentInit(): void {
    console.log('this is ngAfterContentInit');
  }

  ngAfterContentChecked(): void {
    console.log('this is ngAfterContentChecked');
  }

  ngAfterViewInit(): void {
    console.log('this is ngAfterViewInit');
  }

  ngAfterViewChecked(): void {
    console.log('this is ngAfterViewChecked');
  }

  ngOnDestroy(): void {
    console.log('this is ngOnDestroy');
  }

}

  1. 进行属性绑定:
    将这个组件作为子组件放置在其他组件中,这里放在原来的父子组件传值的组件里。

修改组件ParentComComponent模版文件

<nz-row>
  <nz-form-label nz-col nzOffset="3" [nzSpan]="3">
    父组件
  </nz-form-label>
  <nz-form-control nz-col nzSpan="5">
    <button nz-button (click)="parentEvent()">父组件的按钮</button>
  </nz-form-control>
</nz-row>
<hr/>
<nz-row>
  <nz-col nzOffset="3">
    <app-child-com>
      <h2>children h2</h2>
    </app-child-com>
  </nz-col>
</nz-row>

<h2>h1Test</h2>


<nz-divider nzText="Angular生命周期钩子"></nz-divider>
<app-lifecycle-hooks [name]="'Angular生命周期钩子'"></app-lifecycle-hooks>

保存运行:

在这里插入图片描述

ngOnDestroy钩子的调用

当切换页面、ngIf的时候就相当于组件销毁了,就会发现ngOnDestroy钩子的调用.

在这里插入图片描述

Angular生命周期的钩子执行顺序

生命周期钩子接口钩子用途
OnChangesngOnChanges(changes: SimpleChanges)在组件有绑定值的时候,在绑定值发生变化的时候调用,在ngOninit()之前,任何一个、多个绑定值发生变化的时候都会调用,在这个钩子中,可以有一个SimpleChanges类型的参数,这个参数可以看到绑定的是和上一次发生变化,并可以判断是否是第一次变化。
OnInitngOnInit()组件在数据、指令、组件绑定完成以后,初始化组件、指令,在第一轮ngOnChanges调用完了以后才开始调用,特别注意这个只调用一次
DoCheckngDoCheck()在Angular中无法检测或者非正常情况下进行检测变化,紧跟在ngOnChanges()和ngOnInit()后调用
AfterContentInitngAfterContentInit()在Angular将外部内容投影进来以后进行调用,第一次ngDoCheck()调用完成以后才开始调用这个生命周期的钩子,并且这个生命周期的钩子只调用一次
AfterContentCheckedngAfterContentChecked()当Angular完成被投影组件的内容变更检测以后调用,在ngAfterContentInit()和ngDoCheck()之后调用
AfterViewInitngAfterViewInit()当Angular完成组件视图、子组件视图以后调用,第一次ngAfterContentChecked()之后调用,注意只调用一次
AfterViewCheckedngAfterViewChecked()在Angular完成组件的视图、子组件的视图检测以后调用,在ngAfterViewInit() 和每次 ngAfterContentChecked() 之后调用。
OnDestroyngOnDestroy()在Angular销毁组件、指令之前调用并清扫
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值