Angular中的生命周期函数

组件

lifecycle.html

<h2>{{msg}}</h2>
<br>
<button (click)="changeMsg()">改变msg的值</button>
<input type="text" [(ngModel)]="userinfo" />

lifecycle.ts

import { Component,Input} from '@angular/core';

@Component({
  selector: 'app-lifecycle',
  templateUrl: './lifecycle.component.html',
  styleUrls: ['./lifecycle.component.scss']
})
export class LifecycleComponent{


    @Input('title') title:string;


    public msg:string='我是一个生命周期演示';

    public userinfo:string='';

    public oldUserinfo:string='';

    constructor() { 

      console.log('00构造函数执行了---除了使用简单的值对局部变量进行初始化之外,什么都不应该做')
    }

    ngOnChanges() {

      console.log('01ngOnChages执行了---当被绑定的输入属性的值发生变化时调用(父子组件传值的时候会触发)'); 
    }

    ngOnInit() {
        console.log('02ngOnInit执行了--- 请求数据一般放在这个里面');     
    }

    ngDoCheck() {
        //写一些自定义的操作

        console.log('03ngDoCheck执行了---检测,并在发生 Angular 无法或不愿意自己检测的变化时作出反应');
        if(this.userinfo!==this.oldUserinfo){
            console.log(`你从${this.oldUserinfo}改成${this.userinfo}`);
            this.oldUserinfo = this.userinfo;
        }else{
            
            console.log("数据没有变化");          
        }

    }

    ngAfterContentInit() {
        console.log('04ngAfterContentInit执行了---当把内容投影进组件之后调用');
    }
    ngAfterContentChecked() {
        console.log('05ngAfterContentChecked执行了---每次完成被投影组件内容的变更检测之后调用');
    }
    ngAfterViewInit(): void {     
        console.log('06 ngAfterViewInit执行了----初始化完组件视图及其子视图之后调用(dom操作放在这个里面)');
    }
    ngAfterViewChecked() {
        console.log('07ngAfterViewChecked执行了----每次做完组件视图和子视图的变更检测之后调用');
    }


    ngOnDestroy() {
        console.log('08ngOnDestroy执行了····');
    }


    //自定义方法

    changeMsg(){


      this.msg="数据改变了";
    }

}

 

app.component.html

<!--The content below is only a placeholder and can be replaced.-->
<app-lifecycle [title]="title" *ngIf="flag"></app-lifecycle>



<br>

<hr>


<br>


这是父组件

<button (click)="changeTilte()">改变父组件的title</button>


<br>


<br>

<button (click)="changeFlag()">挂载以及卸载组件</button>

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'angulardemo07';

  public flag:boolean=true;


  changeTilte(){


      this.title="改变后的title";
  }

  changeFlag(){

    this.flag=!this.flag;
  }
}

app.module.ts

import { FormsModule } from '@angular/forms';

  imports: [
    FormsModule
  ],
生命周期函数通俗的讲就是组件创建、组件更新、组件销毁的时候会触发的一系列的方法。 
当 Angular 使用构造函数新建一个组件或指令后,就会按下面的顺序在特定时刻调用这些 生命周期钩子方法。
constructor
构造函数中除了使用简单的值对局部变量进行初始化 之外,什么都不应该做。 (非生命周期函数)

ngOnChanges()
当Angular(重新)设置数据绑定输入属性时响应。 该 方法接受当前和上一属性值的 SimpleChanges 对象 当被绑定的输入属性的值发生变化时调用,首次调用一 定会发生在 ngOnInit() 之前

ngOnInit()
在 Angular 第一次显示数据绑定和设置指令/组件的输 入属性之后,初始化指令/组件。 在第一轮 ngOnChanges() 完成之后调用,只调用一次。 使用 ngOnInit() 有两个原因: 1、在构造函数之后马上执行复杂的初始化逻辑 2、在 Angular 设置完输入属性之后,对该组件进行准备。有经验的开发者会认同组件的构建应该很便宜和安全。

ngDoCheck()
检测,并在发生 Angular 无法或不愿意自己检测的变 化时作出反应。在每个 Angular 变更检测周期中调用, ngOnChanges() 和 ngOnInit() 之后

ngAfterContentInit()
当把内容投影进组件之后调用。第一次 ngDoCheck() 之 后调用,只调用一次。

ngAfterContentChecked()
 每次完成被投影组件内容的变更检测之后调用。 ngAfterContentInit() 和每次 ngDoCheck() 之后调用。

ngAfterViewInit()
初 始 化 完 组 件 视 图 及 其 子 视 图 之 后 调 用 。 第 一 次 ngAfterContentChecked() 之后调用,只调用一次

ngAfterViewChecked()
每次做完组件视图和子视图的变更检测之后调用。 ngAfterViewInit()和每次 ngAfterContentChecked() 之后 调用。

ngOnDestroy()
当 Angular 每次销毁指令/组件之前调用并清扫。在这 儿反订阅可观察对象和分离事件处理器,以防内存泄 漏。在 Angular 销毁指令/组件之前调用。

 

 

posted @ 2020-01-07 22:44  生如逆旅,一苇以航  阅读(403)  评论(0编辑  收藏  举报