本文介绍了ionic 2或ionic 2+中的ngOnInit与ionViewDidLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我将使用哪个初始化数据,为什么?

Which one will I use for initializing data and why?

ngOnInit() {
    this.type = 'category';
    this.getData();
    this.setData();
}

ionViewDidLoad() {
    this.type = 'category';
    this.getData();
    this.setData();
}

推荐答案

ngOnInit是Angular2调用的生命周期挂钩,以指示Angular已完成组件的创建.

ngOnInit is a life cycle hook called by Angular2 to indicate that Angular is done creating the component.

ionViewDidLoad与Ionic的NavController lifeCycle事件有关.页面加载后运行.此事件在创建每个页面时仅发生一次.

ionViewDidLoad is related to the Ionic's NavController lifeCycle events. It runs when the page has loaded. This event only happens once per page being created.

基本上,这两个都是初始化组件数据的好地方.

Basically both are good places for initializing the component's data.

但是要使用ngOnInit,您需要实现Angular的OnInit类,另一方面,只能为从NavController推送/弹出的组件定义ionViewDidLoad.

But for using ngOnInit you need to implement the Angular's OnInit class,In the other hand ionViewDidLoad could be only defined for components that are pushed/popped from a NavController.

所以我想说ionViewDidLoad用于NavController堆栈中的组件,而ngOnInit用于其他组件.

So I would say use the ionViewDidLoad for components in the NavController stack and ngOnInit for other components.

这篇关于ionic 2或ionic 2+中的ngOnInit与ionViewDidLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:52