ionic 2 page change event

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

    As of Ionic 3.6, you can use the App component to subscribe to application-wide page change events, as documented in : https://ionicframework.com/docs/api/components/app/App/

    For example, if you'd like to track every view change in Google Analytics using the GA cordova plugin, you could amend your app.component.ts like this :

    constructor(private app: App, private platform: Platform, private ga: GoogleAnalytics, ...) {
      this.platform.ready().then(() => {
        this.ga.startTrackerWithId('UA-XXX').then(() => {
          this.app.viewDidEnter.subscribe((evt) => {
            // evt.instance is the Ionic page component
            this.ga.trackView(evt.instance.title);
          });
        }).catch(e => console.log('Doh', e));
      }
    }
    

提交回复
热议问题