Angular2 constructor VS ngOnInit

constructor和ngOnInit钩子有什么不一样?app

constructor

constructor(构造函数)是ES6类或TypeScript类中的特殊方法,而不是Angular的方法,主要用来作初始化操做,在进行类实例化操做是,会被自动调用。经过constructor方法并不能使咱们知道Angular什么时候完成了组件的初始化工做。函数

仅显示constructor方法:this

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

@Component({})
class ExampleComponent {
  // this is called by the JavaScript engine
  // rather than Angular
  constructor(name) {
    console.log('Constructor initialised');
    this.name = name;
  }
}

// internally calls the constructor
let appCmp = new ExampleComponent('AppCmp');
console.log(appCmp.name);

运行以上代码,控制台输出结果:spa

Constructor initialization
AppCmp

constructor方法是由JavaScript引擎调用的,而不是Angular,这就是为何ngOnInit生命周期钩子被建立的缘由。code

ngOnInit

ngOnInit 是 Angular 2 组件生命周期中的一个钩子,Angular 2 中的全部钩子和调用顺序以下:component

  1. ngOnChanges - 当数据绑定输入属性的值发生变化时调用对象

  2. ngOnInit - 在第一次 ngOnChanges 后调用blog

  3. ngDoCheck - 自定义的方法,用于检测和处理值的改变生命周期

  4. ngAfterContentInit - 在组件内容初始化以后调用ip

  5. ngAfterContentChecked - 组件每次检查内容时调用

  6. ngAfterViewInit - 组件相应的视图初始化以后调用

  7. ngAfterViewChecked - 组件每次检查视图时调用

  8. ngOnDestroy - 指令销毁前调用

其中 ngOnInit 用于在 Angular 获取输入属性后初始化组件,该钩子方法会在第一次 ngOnChanges 以后被调用。

另外须要注意的是 ngOnInit 钩子只会被调用一次,咱们来看一下具体示例:

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

@Component({
  selector: 'my-app',
  template: `
    <h1>Welcome to Angular World</h1>
    <p>Hello {{name}}</p>
  `,
})
export class AppComponent implements OnInit {

  name: string = '';

  constructor() {
    console.log('Constructor initialization');
    this.name = 'Semlinker';
  }

  ngOnInit() {
    console.log('ngOnInit hook has been called');
  }
}

 

以上代码运行后,控制台输出结果:

Constructor initialization
ngOnInit hook has been called

接下来看一个父-子组件传参的例子:

parent.component.ts

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

@Component({
  selector: 'exe-parent',
  template: `
    <h1>Welcome to Angular World</h1>
    <p>Hello {{name}}</p>
    <exe-child [pname]="name"></exe-child>
  `,
})
export class ParentComponent {
  name: string = '';

  constructor() {
    this.name = 'Semlinker';
  }
}

child.component.ts

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

@Component({
    selector: 'exe-child',
    template: `
     <p>父组件的名称:{{pname}} </p>
    `
})
export class ChildComponent implements OnInit {
    @Input()
    pname: string; // 父组件的名称

    constructor() {
        console.log('ChildComponent constructor', this.pname); // Output:undefined
    }

    ngOnInit() {
        console.log('ChildComponent ngOnInit', this.pname);
    }
}

以上代码运行后,控制台输出结果:

ChildComponent constructor undefined
ChildComponent ngOnInit Semlinker

咱们发如今 ChildComponent 构造函数中,是没法获取输入属性的值,而在 ngOnInit 方法中,咱们能正常获取输入属性的值。由于 ChildComponent 组件的构造函数会优先执行,当 ChildComponent 组件输入属性变化时会自动触发 ngOnChanges 钩子,而后在调用 ngOnInit 钩子方法,因此在 ngOnInit 方法内能获取到输入的属性。

constructor 应用场景

在 Angular 2 中,构造函数通常用于依赖注入或执行一些简单的初始化操做。

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

@Component({
  selector: 'my-app',
  template: `
    <h1>Welcome to Angular World</h1>
    <p>Hello {{name}}</p>
  `,
})
export class AppComponent {
  name: string = '';

  constructor(public elementRef: ElementRef) { // 使用构造注入的方式注入依赖对象
    this.name = 'Semlinker'; // 执行初始化操做
  }
}

ngOnInit 应用场景

在项目开发中咱们要尽可能保持构造函数简单明了,让它只执行简单的数据初始化操做,所以咱们会把其余的初始化操做放在 ngOnInit 钩子中去执行。如在组件获取输入属性以后,需执行组件初始化操做等。

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

@Component({})
class ExampleComponent implements OnInit {
  constructor() {}
  
  // called on demand by Angular
  ngOnInit() {
    console.log('ngOnInit fired');
  }
}

const instance = new ExampleComponent();

// Angular calls this when necessary
instance.ngOnInit();
相关文章
相关标签/搜索