Angular2中constructor和ngOninit的使用講解
constructor和ngOninit的使用
Angular中根據(jù)適用場景定義了很多生命周期函數(shù),其本質(zhì)上是事件的響應(yīng)函數(shù),其中最常用的就是ngOnInit。在TypeScript或ES6中還存在著名為constructor的構(gòu)造函數(shù),開發(fā)過程中經(jīng)常會(huì)混淆二者,兩者在含義上有部分重復(fù),下面主要解析一下它們的區(qū)別和各自的使用場景。
區(qū)別
constructor:Es6引入類的概念后出來的東西,是類自身的屬性,并不屬于angular,所以Angular沒有辦法控制constructor。
constructor會(huì)在類生成實(shí)例時(shí)調(diào)用:
import {Component} from '@angular/core';
?
@Component({
? ? selector: 'hello-world',
? ? templateUrl: 'hello-world.html'
})
?
class HelloWorld {
? ? constructor() {
? ? ? ? console.log('constructor被調(diào)用,但和Angular無關(guān)');
? ? }
}
?
// 生成類實(shí)例,此時(shí)會(huì)調(diào)用constructor
new HelloWorld();所以就出現(xiàn)了ngOnInit;
ngOnInit的作用根據(jù)官方的說法:
ngOnInit用于在Angular第一次顯示數(shù)據(jù)綁定和設(shè)置指令/組件的輸入屬性之后,初始化指令/組件
所以總的來說:
1.ngOnChanges當(dāng)數(shù)據(jù)綁定輸入屬性的值發(fā)生變化時(shí)候調(diào)用;
2.ngOnInit( )在第一次ngOnChanges( )后調(diào)用;說明這兩個(gè)方法一般是配合工作的;
3.ngOnInit()用于Angular獲取輸入屬性后初始化組件,此方法是鉤子方法,在ngOnChanges()方法被調(diào)用之后使用;
4.ngOnInit()鉤子只會(huì)被調(diào)用一次;
ngOnInit屬于Angular生命周期的一部分,其在第一輪ngOnChanges完成之后調(diào)用,并且只調(diào)用一次:
import {Component, OnInit} from '@angular/core';
?
@Component({
? ? selector: 'hello-world',
? ? templateUrl: 'hello-world.html'
})
?
class HelloWorld implements OnInit {
? ? constructor() {
?
? ? }
?
? ? ngOnInit() {
? ? ? ? console.log('ngOnInit被Angular調(diào)用');
? ? }
}適用場景
constructor- 即使Angular定義了ngOnInit,constructor也有其用武之地,其主要作用是注入依賴,特別是在TypeScript開發(fā)Angular工程時(shí),經(jīng)常會(huì)遇到類似下面的代碼:
import { Component, ElementRef } from '@angular/core';
?
@Component({
? ? selector: 'hello-world',
? ? templateUrl: 'hello-world.html'
})
class HelloWorld {
? ? constructor(private elementRef: ElementRef) {
? ? ? ? // 在類中就可以使用this.elementRef了
? ? }
}在constructor中注入的依賴,就可以作為類的屬性被使用了。
ngOnInit- ngOnInit純粹是通知開發(fā)者組件/指令已經(jīng)被初始化完成了,此時(shí)組件/指令上的屬性綁定操作以及輸入操作已經(jīng)完成,也就是說在ngOnInit函數(shù)中我們已經(jīng)能夠操作組件/指令中被傳入的數(shù)據(jù)了:
// hello-world.ts
import { Component, Input, OnInit } from '@angular/core';
?
@Component({
? ? selector: 'hello-world',
? ? template: `<p>Hello {{name}}!</p>`
})
class HelloWorld implements OnInit {
? ? @Input()
? ? name: string;
?
? ? constructor() {
? ? ? ? // constructor中還不能獲取到組件/指令中被傳入的數(shù)據(jù)
? ? ? ? console.log(this.name); ? ? // undefined
? ? }
?
? ? ngOnInit() {
? ? ? ? // ngOnInit中已經(jīng)能夠獲取到組件/指令中被傳入的數(shù)據(jù)
? ? ? ? console.log(this.name); ? ? // 傳入的數(shù)據(jù)
? ? }
}所以,我們可以在ngOnInit中做一些初始化的操作,可以在組件的ngOnInit中操作父組件傳遞到子組件的一些shu數(shù)據(jù);
所以,開發(fā)中我們經(jīng)常在ngOnInit做一些初始化的工作,而這些工作盡量要避免在constructor中進(jìn)行,constructor中應(yīng)該只進(jìn)行依賴注入而不是進(jìn)行真正的業(yè)務(wù)操作。
ngOnInit函數(shù)學(xué)習(xí)小記
ngOnInit方法只是初始化angular的組件和指令,并不是真正的dom加載完成
例子
html代碼:

typescript代碼:

這個(gè)時(shí)候oBox1無法獲取到DOM節(jié)點(diǎn),這是因?yàn)閚gOnInit方法只是初始化angular的組件和指令,并不是真正的dom加載完成。
如果要獲取到oBox1,可以在ngAfterViewInit方法里獲取,該方法是指頁面渲染完成之后觸發(fā)!
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動(dòng)畫的方法
本篇文章主要介紹了angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動(dòng)畫的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
AngularJS 輸入驗(yàn)證詳解及實(shí)例代碼
本文主要介紹AngularJS 輸入驗(yàn)證,這里對AngularJS 輸入驗(yàn)證的資料做了整理,并附簡單實(shí)例代碼和效果圖,有需要的小伙伴參考下2016-07-07
Angular5給組件本身的標(biāo)簽添加樣式class的方法
本篇文章主要介紹了Angular 5 給組件本身的標(biāo)簽添加樣式class的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04

