Difference between Constructor and ngOnInit

学习angular技术 ,

疑问1   Difference between Constructor and ngOnInit

https://stackoverflow.com/questions/35763730/difference-between-constructor-and-ngoninit/47900256#47900256

 

https://stackoverflow.com/questions/35845554/angular-2-component-constructor-vs-oninit/35846307#35846307

 

下面这个是中文版

 

https://blog.csdn.net/u010730126/article/details/64486997

 

即使Angular定义了ngOnInitconstructor也有其用武之地,其主要作用是注入依赖,特别是在TypeScript开发Angular工程时,

 

constructor中注入的依赖,就可以作为类的属性被使用了。

 

如果使用了@Component  注解那么 在构造函数中不能和java 似的,注入属性,为什么,请看上一篇 关于 string 的博客,其实我没真正理解。 这也许是angular 强制这么干的,让construct的功能简单唯一。

Constructor is predefined default method of the typescript class. There is no relation between Angular and constructor. Normally we use constructor to define/initialize some variables, but when we have tasks related to Angular's bindings we move to Angular's ngOnInit life cycle hook. ngOnInit is called just after the constructor call. We can also do the same work in the constructor but its preferable to   use ngOnInit to start Angular's binding.

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值