国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解Angular 中 ngOnInit 和 constructor 使用場景

2019-11-19 16:16:52
字體:
來源:轉載
供稿:網友

1. constructor

constructor應該是ES6中明確使用constructor來表示構造函數的,構造函數使用在class中,用來做初始化操作。當包含constructor的類被實例化時,構造函數將被調用。

來看例子:

class AppComponent {  public name: string;  constructor(name) {    console.log('Constructor initialization');    this.name = name;  }}let appCmp = new AppComponent('AppCmp');  // 這時候構造函數將被調用。console.log(appCmp.name);

轉成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鉤子的實現。用來初始化組件。

Angular中生命周期鉤子的調用順序如下:

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

在Angular銷毀指令/組件之前調用。

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

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的執行是在先的。

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

// parent.component.tsimport { 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.tsimport { 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. 應用場景

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

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

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) { // 使用構造注入的方式注入依賴對象  this.name = 'WXY';          // 執行初始化操作 } ngOnInit() {  this.gotId = this.activatedRoute.params.subscribe(params => this.articleId = params['id']); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石渠县| 怀安县| 波密县| 建水县| 徐闻县| 辽阳县| 宁南县| 惠州市| 都安| 西吉县| 祁阳县| 伊春市| 比如县| 杭锦旗| 嘉峪关市| 呼伦贝尔市| 乌海市| 镶黄旗| 香河县| 万山特区| 苍溪县| 双城市| 康马县| 德惠市| 扶绥县| 佳木斯市| 齐河县| 福泉市| 香河县| 社会| 即墨市| 措美县| 潼南县| 峨眉山市| 承德县| 海阳市| 镇原县| 东港市| 东源县| 绿春县| 当雄县|