美文网首页
angular中的生命周期

angular中的生命周期

作者: nzjcnjzx | 来源:发表于2018-12-07 11:22 被阅读0次

生命周期

1、Angular每个组件都存在一个生命周期,从创建,变更到销毁。Angular提供组件生命周期钩子,把这些关键时刻暴露出来,赋予在这些关键结点和组件进行交互的能力,掌握生命周期,可以让我们更好的开发Angular应用
2、每个接口都有唯一的一个钩子方法,它们的名字是由接口名再加上ng前缀构成的,比如OnInit接口的钩子方法叫做ngOnInit.
3、没有指令或者组件会实现所有这些接口,并且有些钩子只对组件有意义。只有在指令/组件中定义过的那些钩子方法才会被Angular调用。

基于指令与组件的区别来分类:

1、指令与组件共有的钩子:
ngOnChanges
ngOnInit
ngDoCheck
ngOnDestroy
2、组件特有的钩子
ngAfterContentInit
ngAfterContentChecked
ngAfterViewInit
ngAfterViewChecked

Angular 2 指令生命周期钩子的作用及调用顺序

0、constructor 构造函数这里是唯一的依赖注入的地方,它主要用于依赖注入或执行简单的数据初始化操作。
1、ngOnChanges - 当数据绑定输入属性的值发生变化时调用
  2、ngOnInit - 在第一次 ngOnChanges 后调用
  3、ngDoCheck - 自定义的方法,用于检测和处理值的改变
  4、ngAfterContentInit - 在组件内容初始化之后调用
  5、ngAfterContentChecked - 组件每次检查内容时调用
  6、ngAfterViewInit - 组件相应的视图初始化之后调用
  7、ngAfterViewChecked - 组件每次检查视图时调用
  8、ngOnDestroy - 指令销毁前调用

export class HomeComponent implements OnInit, OnChanges{
  public canvasImg = ''
  public count = 0;
  constructor() {
    console.log(html2canvas)
    console.log(++this.count+ '--constructor')
   }
  ngOnChanges(change: SimpleChanges ){
    console.log(111) 
    console.log(change)
    console.log(++this.count+ '--ngOnChanges')

  }
  ngOnInit(): void {
  //Called after the constructor, initializing input properties, and the first call to ngOnChanges.
  //Add 'implements OnInit' to the class.
  console.log(++this.count+ '--ngOnInit')
  
  }
  ngDoCheck(): void {
   //Called every time that the input properties of a component or a directive are checked. Use it to extend change detection by performing a custom check.
   //Add 'implements DoCheck' to the class.
   console.log(++this.count+ '--ngDoCheck')
   
 }
  ngOnDestroy(): void {
  //Called once, before the instance is destroyed.
  //Add 'implements OnDestroy' to the class.
  console.log(++this.count+ '--ngOnDestroy')
  
  }
  ngAfterContentInit(): void {
    //Called after ngOnInit when the component's or directive's content has been initialized.
    //Add 'implements AfterContentInit' to the class.
    console.log(++this.count+ '--ngAfterContentInit')
    
  }
  ngAfterContentChecked(): void {
    //Called after every check of the component's or directive's content.
    //Add 'implements AfterContentChecked' to the class.
    console.log(++this.count+ '--ngAfterContentChecked')
    
  }
  ngAfterViewInit(): void {
    //Called after ngAfterContentInit when the component's view has been initialized. Applies to components only.
    //Add 'implements AfterViewInit' to the class.
    console.log(++this.count+ '--ngAfterViewInit')
    
  }

  ngAfterViewChecked(): void {
    //Called after every check of the component's view. Applies to components only.
    //Add 'implements AfterViewChecked' to the class.
    console.log(++this.count+ '--ngAfterViewChecked')
    
  }
image.png

tips:
ngOnchanges 的调用是发生在有可变值传入的时候会在oninit之前调用 每次传入的值发生改变的时候也会调用。
@input() value:string; 绑定属性[value]="123" 这样的类型不会触发ngOnchanges
[value]="对象" 会触发ngonchanges 在SimpleChanges 对象中保存着当前的值


image.png

相关文章

  • angular 钩子

    angular 钩子ngOnInit是 Angular 组件生命周期中的一个钩子,Angular 中的所有钩子和调...

  • 生命周期

    生命周期和钩子函数 Angular 中每个 component/directive 都有它自己的生命周期。包括创建...

  • angular生命周期

    大纲 1、angular生命周期是什么2、生命周期钩子分类3、Angular 2 指令生命周期钩子的作用及调用顺序...

  • angular6.x--生命周期

    按照生命周期执行的先后顺序,Angular生命周期接口如下所示 生命周期顺序简写在Angular通过构造函数创建组...

  • angular中的生命周期

    生命周期 1、Angular每个组件都存在一个生命周期,从创建,变更到销毁。Angular提供组件生命周期钩子,把...

  • Angular 2+ 的组件生命周期

    参考资料: angular 2.0 从0到1 -> #Angular 2的组件生命周期

  • 12.《Angular生命周期》

    一、生命周期钩子 每个组件都有一个被 Angular 管理的生命周期。Angular 创建它,渲染它,创建并渲染它...

  • angular2+系列——生命周期

    概述 Angular2+(后面就直接用Angular代替了)中的每个组件都存在一个生命周期,从创建,变更到销毁。A...

  • Angular 中的生命周期

    1、编译阶段 第一个阶段是编译阶段。在编译阶段,AngularJS会遍历整个HTML文档并根据JavaScript...

  • Angular更新机制(一):Angular的生命周期

    Angular更新机制(一):Angular的生命周期 了解Angular的更新机制之前,首先需要了解Angula...

网友评论

      本文标题:angular中的生命周期

      本文链接:https://www.haomeiwen.com/subject/ywtthqtx.html