亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Angular 中 ngOnInit 和 constructor 使用場景

 更新時間:2017年06月22日 14:15:25   作者:Keriy  
最初學(xué)習(xí)Angular的時候總是搞不清楚ngOnInit和constructor的區(qū)別,現(xiàn)在我們來稍微理一下兩者之間的區(qū)別。

1. constructor

constructor應(yīng)該是ES6中明確使用constructor來表示構(gòu)造函數(shù)的,構(gòu)造函數(shù)使用在class中,用來做初始化操作。當(dāng)包含constructor的類被實例化時,構(gòu)造函數(shù)將被調(diào)用。

來看例子:

class AppComponent {
  public name: string;
  constructor(name) {
    console.log('Constructor initialization');
    this.name = name;
  }
}

let appCmp = new AppComponent('AppCmp');  // 這時候構(gòu)造函數(shù)將被調(diào)用。
console.log(appCmp.name);

轉(zhuǎn)成ES5代碼如下:

var AppComponent = (function () {
  function AppComponent(name) {
    console.log('Constructor initialization');
    this.name = name;
  }
  return AppComponent;  // 這里直接返回一個實例
}());
var appCmp = new AppComponent('AppCmp');
console.log(appCmp.name);

2. ngOnInit

ngOnInitAngularOnInit鉤子的實現(xiàn)。用來初始化組件。

Angular中生命周期鉤子的調(diào)用順序如下:

  1. ngOnChanges -- 當(dāng)被綁定的輸入屬性的值發(fā)生變化時調(diào)用,首次調(diào)用一定會發(fā)生在ngOnInit()之前。
  2. ngOnInit() -- 在Angular第一次顯示數(shù)據(jù)綁定和設(shè)置指令/組件的輸入屬性之后,初始化指令/組件。在第一輪ngOnChanges()完成之后調(diào)用,只調(diào)用一次。
  3. ngDoCheck -- 自定義的方法,用于檢測和處理值的改變。
  4. ngAfterContentInit -- 在組件內(nèi)容初始化之后調(diào)用,只適用于組件
  5. ngAfterContentChecked -- 組件每次檢查內(nèi)容時調(diào)用,只適用于組件
  6. ngAfterViewInit -- 組件相應(yīng)的視圖初始化之后調(diào)用,只適用于組件
  7. ngAfterViewChecked -- 組件每次檢查視圖時調(diào)用,只適用于組件
  8. ngOnDestroy -- 當(dāng)Angular每次銷毀指令/組件之前調(diào)用并清掃。在這兒反訂閱可觀察對象和分離事件處理器,以防內(nèi)存泄漏。

在Angular銷毀指令/組件之前調(diào)用。

了解了這些之后我們來看一個例子:

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

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

 constructor() {
  console.log('Constructor initialization');
 }

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

這里輸出的是:

Constructor initialization
ngOnInit hook has been called

可以看出,constructor的執(zhí)行是在先的。

那么既然ngOnchanges是輸入屬性值變化的時候調(diào)用,并且ngOnInit是在ngOnchanges執(zhí)行完之后才調(diào)用,而constructor是在組件就實例化的時候就已經(jīng)調(diào)用了,這也就是說,在constructor中我們是取不到輸入屬性的值的。
所以還是看例子:

// 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 = 'God eyes';
 }
}

// 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); // this.name=undefined
  }

  ngOnInit() {
    console.log('ChildComponent ngOnInit', this.pname); // this.name=God eyes
  }
}

一目了然。

3. 應(yīng)用場景

看完的上面的部分可以發(fā)現(xiàn),在constructor中不適合進(jìn)行任何與組件通信類似的復(fù)雜操作,一般在constructor中值進(jìn)行一些簡單的初始化工作:依賴注入,變量初始化等。

那么用到組件間通信的方法我們可以放在ngOnInit中去執(zhí)行,比如異步請求等:

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

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

 constructor(public elementRef: ElementRef) { // 使用構(gòu)造注入的方式注入依賴對象
  this.name = 'WXY';          // 執(zhí)行初始化操作
 }

 ngOnInit() {
  this.gotId = this.activatedRoute.params.subscribe(params => this.articleId = params['id']);
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在angularJs中進(jìn)行數(shù)據(jù)遍歷的2種方法

    在angularJs中進(jìn)行數(shù)據(jù)遍歷的2種方法

    今天小編就為大家分享一篇在angularJs中進(jìn)行數(shù)據(jù)遍歷的2種方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 利用Jasmine對Angular進(jìn)行單元測試的方法詳解

    利用Jasmine對Angular進(jìn)行單元測試的方法詳解

    單元測試是一種能夠幫助開發(fā)者驗證代碼中某一部分有效性的技術(shù)。下面這篇文章主要給大家介紹了關(guān)于利用Jasmine對Angular進(jìn)行單元測試的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • Angular.js自動化測試之protractor詳解

    Angular.js自動化測試之protractor詳解

    Protractor是一個建立在WebDriverJS基礎(chǔ)上的端到端(E2E)的AngularJS JavaScript Web應(yīng)用程序測試框架,下面這篇文章主要給大家介紹了angular.js自動化測試之protractor的相關(guān)資料,需要的朋友可以參考下。
    2017-07-07
  • angular2中Http請求原理與用法詳解

    angular2中Http請求原理與用法詳解

    這篇文章主要介紹了angular2中Http請求原理與用法,結(jié)合實例形式分析了AngularJS中http相關(guān)模塊實現(xiàn)http服務(wù)請求與相應(yīng)的相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • angularJS實現(xiàn)不同視圖同步刷新詳解

    angularJS實現(xiàn)不同視圖同步刷新詳解

    今天小編就為大家分享一篇angularJS實現(xiàn)不同視圖同步刷新詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 詳解angularJs中關(guān)于ng-class的三種使用方式說明

    詳解angularJs中關(guān)于ng-class的三種使用方式說明

    本篇文章主要介紹了angularJs中關(guān)于ng-class的三種使用方式說明,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 詳解AngularJS用Interceptors來統(tǒng)一處理HTTP請求和響應(yīng)

    詳解AngularJS用Interceptors來統(tǒng)一處理HTTP請求和響應(yīng)

    本篇文章主要介紹了AngularJS用Interceptors來統(tǒng)一處理HTTP請求和響應(yīng) ,具有一定的參考價值,有興趣的可以了解一下
    2017-06-06
  • AngularJS使用ocLazyLoad實現(xiàn)js延遲加載

    AngularJS使用ocLazyLoad實現(xiàn)js延遲加載

    這篇文章主要介紹了AngularJS使用ocLazyLoad實現(xiàn)js延遲加載的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Angular 4依賴注入學(xué)習(xí)教程之組件服務(wù)注入(二)

    Angular 4依賴注入學(xué)習(xí)教程之組件服務(wù)注入(二)

    大家都知道依賴注入式AngularJS的重要特性之一,之前我們已經(jīng)介紹了關(guān)于Angular 4依賴注入基礎(chǔ)的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于Angular 4依賴注入之組件服務(wù)注入的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • Angular項目如何使用攔截器?httpClient?請求響應(yīng)處理

    Angular項目如何使用攔截器?httpClient?請求響應(yīng)處理

    這篇文章主要介紹了Angular項目簡單使用攔截器httpClient請求響應(yīng)處理,目前我的Angular版本是Angular?17.3,版本中實現(xiàn)請求和響應(yīng)的攔截處理了,這種機制非常適合添加如身份驗證頭、錯誤統(tǒng)一處理、日志記錄等功能,需要的朋友可以參考下
    2024-06-06

最新評論