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
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));
}
}