詳解Angular 中 ngOnInit 和 constructor 使用場景
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
ngOnInit
是Angular
中OnInit
鉤子的實現(xiàn)。用來初始化組件。
Angular中生命周期鉤子的調(diào)用順序如下:
- ngOnChanges -- 當(dāng)被綁定的輸入屬性的值發(fā)生變化時調(diào)用,首次調(diào)用一定會發(fā)生在ngOnInit()之前。
- ngOnInit() -- 在Angular第一次顯示數(shù)據(jù)綁定和設(shè)置指令/組件的輸入屬性之后,初始化指令/組件。在第一輪ngOnChanges()完成之后調(diào)用,只調(diào)用一次。
- ngDoCheck -- 自定義的方法,用于檢測和處理值的改變。
- ngAfterContentInit -- 在組件內(nèi)容初始化之后調(diào)用,只適用于組件
- ngAfterContentChecked -- 組件每次檢查內(nèi)容時調(diào)用,只適用于組件
- ngAfterViewInit -- 組件相應(yīng)的視圖初始化之后調(diào)用,只適用于組件
- ngAfterViewChecked -- 組件每次檢查視圖時調(diào)用,只適用于組件
- 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種方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10利用Jasmine對Angular進(jìn)行單元測試的方法詳解
單元測試是一種能夠幫助開發(fā)者驗證代碼中某一部分有效性的技術(shù)。下面這篇文章主要給大家介紹了關(guān)于利用Jasmine對Angular進(jìn)行單元測試的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06詳解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) ,具有一定的參考價值,有興趣的可以了解一下2017-06-06AngularJS使用ocLazyLoad實現(xiàn)js延遲加載
這篇文章主要介紹了AngularJS使用ocLazyLoad實現(xiàn)js延遲加載的相關(guān)資料,需要的朋友可以參考下2017-07-07Angular 4依賴注入學(xué)習(xí)教程之組件服務(wù)注入(二)
大家都知道依賴注入式AngularJS的重要特性之一,之前我們已經(jīng)介紹了關(guān)于Angular 4依賴注入基礎(chǔ)的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于Angular 4依賴注入之組件服務(wù)注入的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06Angular項目如何使用攔截器?httpClient?請求響應(yīng)處理
這篇文章主要介紹了Angular項目簡單使用攔截器httpClient請求響應(yīng)處理,目前我的Angular版本是Angular?17.3,版本中實現(xiàn)請求和響應(yīng)的攔截處理了,這種機制非常適合添加如身份驗證頭、錯誤統(tǒng)一處理、日志記錄等功能,需要的朋友可以參考下2024-06-06