[英]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.