ngOnInit vs ionViewDidLoad in ionic 2 or ionic 2+

后端 未结 3 1295
借酒劲吻你
借酒劲吻你 2020-12-29 22:45

Which one will I use for initializing data and why?

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

ionViewDidLoad()          


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 23:02

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

    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.

    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.

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

提交回复
热议问题