詳解Angular 中 ngOnInit 和 constructor 使用場(chǎng)景
1. constructor
constructor應(yīng)該是ES6中明確使用constructor來表示構(gòu)造函數(shù)的,構(gòu)造函數(shù)使用在class中,用來做初始化操作。當(dāng)包含constructor的類被實(shí)例化時(shí),構(gòu)造函數(shù)將被調(diào)用。
來看例子:
class AppComponent {
public name: string;
constructor(name) {
console.log('Constructor initialization');
this.name = name;
}
}
let appCmp = new AppComponent('AppCmp'); // 這時(shí)候構(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; // 這里直接返回一個(gè)實(shí)例
}());
var appCmp = new AppComponent('AppCmp');
console.log(appCmp.name);
2. ngOnInit
ngOnInit是Angular中OnInit鉤子的實(shí)現(xiàn)。用來初始化組件。
Angular中生命周期鉤子的調(diào)用順序如下:
- ngOnChanges -- 當(dāng)被綁定的輸入屬性的值發(fā)生變化時(shí)調(diào)用,首次調(diào)用一定會(huì)發(fā)生在ngOnInit()之前。
- ngOnInit() -- 在Angular第一次顯示數(shù)據(jù)綁定和設(shè)置指令/組件的輸入屬性之后,初始化指令/組件。在第一輪ngOnChanges()完成之后調(diào)用,只調(diào)用一次。
- ngDoCheck -- 自定義的方法,用于檢測(cè)和處理值的改變。
- ngAfterContentInit -- 在組件內(nèi)容初始化之后調(diào)用,只適用于組件
- ngAfterContentChecked -- 組件每次檢查內(nèi)容時(shí)調(diào)用,只適用于組件
- ngAfterViewInit -- 組件相應(yīng)的視圖初始化之后調(diào)用,只適用于組件
- ngAfterViewChecked -- 組件每次檢查視圖時(shí)調(diào)用,只適用于組件
- ngOnDestroy -- 當(dāng)Angular每次銷毀指令/組件之前調(diào)用并清掃。在這兒反訂閱可觀察對(duì)象和分離事件處理器,以防內(nèi)存泄漏。
在Angular銷毀指令/組件之前調(diào)用。
了解了這些之后我們來看一個(gè)例子:
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是輸入屬性值變化的時(shí)候調(diào)用,并且ngOnInit是在ngOnchanges執(zhí)行完之后才調(diào)用,而constructor是在組件就實(shí)例化的時(shí)候就已經(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)用場(chǎng)景
看完的上面的部分可以發(fā)現(xiàn),在constructor中不適合進(jìn)行任何與組件通信類似的復(fù)雜操作,一般在constructor中值進(jìn)行一些簡(jiǎn)單的初始化工作:依賴注入,變量初始化等。
那么用到組件間通信的方法我們可以放在ngOnInit中去執(zhí)行,比如異步請(qǐng)求等:
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)造注入的方式注入依賴對(duì)象
this.name = 'WXY'; // 執(zhí)行初始化操作
}
ngOnInit() {
this.gotId = this.activatedRoute.params.subscribe(params => this.articleId = params['id']);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在angularJs中進(jìn)行數(shù)據(jù)遍歷的2種方法
今天小編就為大家分享一篇在angularJs中進(jìn)行數(shù)據(jù)遍歷的2種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
利用Jasmine對(duì)Angular進(jìn)行單元測(cè)試的方法詳解
單元測(cè)試是一種能夠幫助開發(fā)者驗(yàn)證代碼中某一部分有效性的技術(shù)。下面這篇文章主要給大家介紹了關(guān)于利用Jasmine對(duì)Angular進(jìn)行單元測(cè)試的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
Angular.js自動(dòng)化測(cè)試之protractor詳解
Protractor是一個(gè)建立在WebDriverJS基礎(chǔ)上的端到端(E2E)的AngularJS JavaScript Web應(yīng)用程序測(cè)試框架,下面這篇文章主要給大家介紹了angular.js自動(dòng)化測(cè)試之protractor的相關(guān)資料,需要的朋友可以參考下。2017-07-07
angularJS實(shí)現(xiàn)不同視圖同步刷新詳解
今天小編就為大家分享一篇angularJS實(shí)現(xiàn)不同視圖同步刷新詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
詳解angularJs中關(guān)于ng-class的三種使用方式說明
本篇文章主要介紹了angularJs中關(guān)于ng-class的三種使用方式說明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
詳解AngularJS用Interceptors來統(tǒng)一處理HTTP請(qǐng)求和響應(yīng)
本篇文章主要介紹了AngularJS用Interceptors來統(tǒng)一處理HTTP請(qǐng)求和響應(yīng) ,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06
AngularJS使用ocLazyLoad實(shí)現(xiàn)js延遲加載
這篇文章主要介紹了AngularJS使用ocLazyLoad實(shí)現(xiàn)js延遲加載的相關(guān)資料,需要的朋友可以參考下2017-07-07
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項(xiàng)目如何使用攔截器?httpClient?請(qǐng)求響應(yīng)處理
這篇文章主要介紹了Angular項(xiàng)目簡(jiǎn)單使用攔截器httpClient請(qǐng)求響應(yīng)處理,目前我的Angular版本是Angular?17.3,版本中實(shí)現(xiàn)請(qǐng)求和響應(yīng)的攔截處理了,這種機(jī)制非常適合添加如身份驗(yàn)證頭、錯(cuò)誤統(tǒng)一處理、日志記錄等功能,需要的朋友可以參考下2024-06-06

