Angular的生命周期

组件的生命周期和指令的生命的周期唯一区别就是指令没有AfterContentInit和AfterContentChecked,因为指令没有投影。在组件或父组件发生变更检测的时候都会调用三个变更检测方法:

ngDoCheck、ngAfterContentChecked、ngAfterViewChecked

以下生成的生命周期方法即为组件生命周期执行顺序

import {
AfterContentChecked,
AfterContentInit, AfterViewChecked,
AfterViewInit,
Component,
DoCheck, Input,
OnChanges, OnDestroy,
OnInit
} from '@angular/core';

@Component({
selector: 'app-life-cycle',
templateUrl: './life-cycle.component.html',
styleUrls: ['./life-cycle.component.scss']
})
export class LifeCycleComponent implements OnChanges,OnInit,DoCheck,AfterContentInit,AfterContentChecked,AfterViewInit,AfterViewChecked,OnDestroy {

@Input() title: string = "def";

//在构造函数里不能获取最新的输入属性值
constructor() {
 console.log("constructor",this.title);
}

//在输入属性发生变化的时候调用
ngOnChanges(): void {
 console.log("ngOnChanges",this.title);
}

//属性和指令初始化完成调用
ngOnInit(): void {
 console.log("ngOnInit");
}

//首次变更检测完调用
ngDoCheck(): void {
 console.log("ngDoCheck");
}

//投影初始化完调用
ngAfterContentInit(): void {
 console.log("ngAfterContentInit");
}

//投影变更检测完调用
ngAfterContentChecked(): void {
 console.log("ngAfterContentChecked");
}

//视图初始化完调用
ngAfterViewInit(): void {
 console.log("ngAfterViewInit");
}

//视图变更检测完调用
ngAfterViewChecked(): void {
 console.log("ngAfterViewChecked");
}

//组件销毁完调用
ngOnDestroy(): void {
 console.log("ngOnDestroy");
}

}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值