Angular开发(七)-关于组件的生命周期

一、说明

Angular每个组件都存在一个生命周期,从创建,变更到销毁。Angular提供组件生命周期钩子,把这些关键时刻暴露出来,赋予在这些关键结点和组件进行交互的能力。

二、angular中总共有8个生命周期钩子函数下面统一介绍

名称调用时机接口名称范围
ngOnChanges当被绑定的输入属性的值发生变化时调用,首次调用一定会发生在 ngOnInit之前。OnChanges指令和组件
ngOnInit在第一轮 ngOnChanges 完成之后调用。 ( 译注:也就是说当每个输入属性的值都被触发了一次 ngOnChanges 之后才会调用 ngOnInit ,此时所有输入属性都已经有了正确的初始绑定值 )OnInit指令和组件
ngDoCheck在每个 Angular 变更检测周期中调用。DoCheck指令和组件
ngAfterContentInit当把内容投影进组件之后调用。AfterContentInit组件
ngAfterContentChecked每次完成被投影组件内容的变更检测之后调用。AfterContentChecked组件
ngAfterViewInit初始化完组件视图及其子视图之后调用。after initializing the component’s views and child views.AfterViewInit组件
ngAfterViewChecked每次做完组件视图和子视图的变更检测之后调用。AfterViewChecked组件
ngOnDestroy当 Angular 每次销毁指令 / 组件之前调用。OnDestroy指令和组件

三、查看调用顺序

//在app.component.html代码
<input type="text" [(ngModel)]="name" />
<app-life [item]="name"></app-life>
//在app.component.ts代码
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name:any = "";
}
//在life.component.html代码
<p>
    我是life组件
</p>
<p>我是从父组件输入的内容:{{item}}</p>
//在life.component.ts代码
import { Component, OnInit,Input,OnChanges,SimpleChanges,DoCheck,AfterContentInit,AfterContentChecked,AfterViewInit,AfterViewChecked,OnDestroy} from '@angular/core';

@Component({
  selector: 'app-life',
  templateUrl: './life.component.html',
  styleUrls: ['./life.component.css']
})
export class LifeComponent implements OnInit,OnChanges,DoCheck,AfterContentInit,AfterContentChecked,AfterViewInit,AfterViewChecked,OnDestroy {
  @Input() item:any = "";
  index:number = 0;
  log(arg):void{
      console.log(`#${this.index}我是${arg}内容`);
      this.index ++;
  }
  constructor() {
      this.log("constructor");
  }

  ngOnInit() {
    this.log("ngOnInit");
  }
  ngOnChanges(changes:SimpleChanges){
    this.log("ngOnChanges");
  }
  ngDoCheck(){
    this.log("ngDoCheck");
  }
  ngAfterContentInit(){
    this.log("ngAfterContentInit");
  }
  ngAfterContentChecked(){
    this.log("ngAfterContentChecked");
  }
  ngAfterViewInit(){
    this.log("ngAfterViewInit");
  }
  ngAfterViewChecked(){
    this.log("ngAfterViewChecked");
  }
  ngOnDestroy(){
    this.log("ngOnDestroy");
  }
}
  • 1、项目初始化后运行效果如下:
    生命周期运行效果图

  • 2、当用户在输入框输入的时候,传递数据到子组件中运行效果如下:
    这里写图片描述

  • 3、当鼠标离开输入框的时候谷歌控制台的效果
    这里写图片描述
  • 4、angular中变化检测的钩子函数有ngOnChanges、ngDocheck、ngAfterContentChecked、ngAfterViewChecked

四、ngOnChanges的介绍

  • 1、在父组件初始化
  • 2、修改子组件
    在上面两种情况下会触发ngOnChanges钩子函数,把上面的代码修改下直接打印输出值
ngOnChanges(changes:SimpleChanges){
    console.log(JSON.stringify(changes,null,2));
  }

运行输出结果,会展示一个对象,显示当前值与上一次的值
运行输出结果

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水痕01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值