ionic 2 page change event

前端 未结 6 2504
深忆病人
深忆病人 2021-02-18 20:38

I want to execute some code every time the page changes.

I could add add an ngOnDestroy method to every page. It appears that I could use Ionic 2 page lifec

6条回答
  •  北海茫月
    2021-02-18 21:18

    Ionic2 doesn't use Angular2's Router. They have their own implementation NavController.


    I see that you can subscribe to Angular 2 Router events. How do I translate that for use in Ionic 2?

    You can merge all the NavController Events and subscribe to it.

    allEvents = Observable.merge(
                     this.navController.viewDidLoad, 
                     this.navController.viewWillEnter, 
                     this.navController.viewDidEnter, 
                     this.navController.viewWillLeave, 
                     this.navController.viewDidLeave, 
                     this.navController.viewWillUnload);
    
    allEvents.subscribe((e) => {
        console.log(e);
    });
    

提交回复
热议问题