ionic 2 page change event

前端 未结 6 2508
深忆病人
深忆病人 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:17

    You can use plain old Javascript to be placed in app.component.ts. Assuming that you use Ionic2-RC0:

    import { Component } from '@angular/core';
    import { Platform } from 'ionic-angular';
    import { StatusBar } from 'ionic-native';
    
    import { YourPage } from '../pages/yourpage/yourpage';
    
    @Component({
      template: ``
    })
    export class MyApp {
      rootPage = YourPage;
    
      constructor(platform: Platform) {
        platform.ready().then(() => {
    
          // Okay, so the platform is ready and our plugins are available.
          // Here you can do any higher level native things you might need.
          StatusBar.styleDefault();
          window.addEventListener('load', () =>{
             console.log('page changed');
          });
        });
      }
    

    Every time you change page using the NavController, you'll have page changed printed out in the console.

提交回复
热议问题