简体   繁体   English

构造函数和 ngOnInIt 有什么区别?

[英]What is difference between constructor and ngOnInIt?

It seems to be similar functions, what are the main differences and purposes of them好像是类似的功能,它们的主要区别和用途是什么

//Code //代码

import { OnInit } from '@angular/core';
constructor(){


}

ngOnInit(){

}

People have already mentioned life cycle hooks so I won't talk about that.人们已经提到了生命周期钩子,所以我不会谈论那个。 However, Angular docs state that in the constructor you should only do lightweight operations such as simple sync variable assignments.但是,Angular 文档声明在构造函数中您应该只执行轻量级操作,例如简单的同步变量赋值。

In ngOnInit you should do more heavy lifting operations such as HTTP requests, etc在 ngOnInit 中,您应该做更多繁重的操作,例如 HTTP 请求等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular中构造函数和ngOnInit()之间的逻辑混淆 - Confusion about Logic between constructor and ngOnInit() in Angular 这两种构造器模式有什么区别? - What is the difference between these two constructor patterns? Javascript-这些构造函数之间有什么区别? - Javascript - What is the difference between these constructor functions? Proxy构造函数和Reflect有什么区别? - What is the difference between Proxy constructor and Reflect? Object构造函数和Global Object之间有什么区别 - What is the difference between Object constructor and Global Object 函数构造函数和原型构造函数有什么区别? - What's the difference between a function constructor and prototype constructor? 构造函数创建后,构造函数中的方法与函数上的方法有什么区别 - What is difference between method in constructor and method on function after constructor created 使用布尔构造函数的类型强制和内部if有什么区别 - what is the difference between type coercion using Boolean constructor function and inside if javascript中的接口和构造函数有什么区别? - What's the difference between an interface and a constructor function in javascript? 构造函数 function 和 function 返回 object 之间有什么区别? - What is the difference between a constructor function and function returning an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM