详解Angular之constructor和ngOnInit差异及适用场景

作者:刘文壮 时间:2024-05-11 09:18:16 

Angular中根据适用场景定义了很多生命周期函数,其本质上是事件的响应函数,其中最常用的就是ngOnInit。但在TypeScript或ES6中还存在着名为constructor的构造函数,开发过程中经常会混淆二者,毕竟它们的含义有某些重复部分,那ngOnInit和constructor之间有什么区别呢?它们各自的适用场景又是什么呢?

区别

constructor是ES6引入类的概念后新出现的东东,是类的自身属性,并不属于Angular的范畴,所以Angular没有办法控制constructor。constructor会在类生成实例时调用:


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

@Component({
 selector: 'hello-world',
 templateUrl: 'hello-world.html'
})

class HelloWorld {
 constructor() {
   console.log('constructor被调用,但和Angular无关');
 }
}

// 生成类实例,此时会调用constructor
new HelloWorld();

既然Angular无法控制constructor,那么ngOnInit的出现就不足为奇了,毕竟枪把子得握在自己手里才安全。

ngOnInit的作用根据官方的说法:

ngOnInit用于在Angular第一次显示数据绑定和设置指令/组件的输入属性之后,初始化指令/组件。

ngOnInit属于Angular生命周期的一部分,其在第一轮ngOnChanges完成之后调用,并且只调用一次:


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

@Component({
 selector: 'hello-world',
 templateUrl: 'hello-world.html'
})

class HelloWorld implements OnInit {
 constructor() {

}

ngOnInit() {
   console.log('ngOnInit被Angular调用');
 }
}

constructor适用场景

即使Angular定义了ngOnInit,constructor也有其用武之地,其主要作用是注入依赖,特别是在TypeScript开发Angular工程时,经常会遇到类似下面的代码:


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纯粹是通知开发者组件/指令已经被初始化完成了,此时组件/指令上的属性绑定操作以及输入操作已经完成,也就是说在ngOnInit函数中我们已经能够操作组件/指令中被传入的数据了:


// 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中还不能获取到组件/指令中被传入的数据
   console.log(this.name);   // undefined
 }

ngOnInit() {
   // ngOnInit中已经能够获取到组件/指令中被传入的数据
   console.log(this.name);   // 传入的数据
 }
}

所以我们可以在ngOnInit中做一些初始化操作。

总结

开发中我们经常在ngOnInit做一些初始化的工作,而这些工作尽量要避免在constructor中进行,constructor中应该只进行依赖注入而不是进行真正的业务操作。

来源:http://blog.csdn.net/u010730126/article/details/64486997

标签:angular,constructor,ngOnInit
0
投稿

猜你喜欢

  • 六个窍门助你提高Python运行效率

    2021-07-19 08:31:15
  • Zabbix实现微信报警功能

    2021-10-17 04:40:05
  • ECMAScript6函数默认参数

    2024-05-13 09:18:24
  • python使用turtle库绘制树

    2022-04-14 09:09:06
  • asp如何利用当前时间生成随机函数?

    2010-01-01 15:44:00
  • Python制作简单的网页爬虫

    2022-10-09 12:49:16
  • python中绑定方法与非绑定方法的实现示例

    2021-01-03 03:59:13
  • 简单的MySQL备份与还原方法分享

    2024-01-24 22:51:13
  • Python使用read_csv读数据遇到分隔符问题的2种解决方式

    2022-01-13 13:30:47
  • 基于jQuery的自动完成插件

    2011-02-05 10:55:00
  • 安装python-docx后,无法在pycharm中导入的解决方案

    2022-09-08 10:08:13
  • python 获取等间隔的数组实例

    2023-05-21 15:07:16
  • Python字符串本身作为bytes进行解码的问题

    2022-12-22 07:18:42
  • 工程师必须了解的LRU缓存淘汰算法以及python实现过程

    2023-05-22 22:46:00
  • PHP之Mysql常用SQL语句示例的深入分析

    2024-05-05 09:31:21
  • Navicat15安装教程超详细步骤(最靠谱)

    2024-01-12 22:29:11
  • 基于Python实现股票数据分析的可视化

    2021-06-04 16:11:36
  • Python 日期时间datetime 加一天,减一天,加减一小时一分钟,加减一年

    2023-08-08 12:12:04
  • Python常用模块用法分析

    2022-02-05 05:29:43
  • 176万!GPT-4发布详解如何查看OpenAI的下载量

    2021-11-26 22:02:51
  • asp之家 网络编程 m.aspxhome.com