Hello im trying to call globaly function on all view page, its possible on ionic ? im add ionViewWillEnter on app.component.ts still not working
ionViewWillEnter(){ console.log('aaa'); }
thankyou
Hello im trying to call globaly function on all view page, its possible on ionic ? im add ionViewWillEnter on app.component.ts still not working
ionViewWillEnter(){ console.log('aaa'); }
thankyou
app.component.ts has no view so you can't use ionViewWillEnter(). You can use ngOnInit.
this can also help https://stackoverflow.com/questions/39420241/angular-2-global-lifecycle-hooks
Here is my solution you can consider for your project. I use NavigationStart
and NavigationEnd
events in Router instead of ionViewWillEnter
and ionViewDidEnter
.
Please review Ionic Page Life Cycle:
Begin Navigation -> ngOnInit -> Begin page transition -> ionViewWillEnter -> Page fully transitioned.
app.components.ts
import {
Router,
NavigationStart,
NavigationEnd,
Event as NavigationEvent,
} from "@angular/router";
class Example {
constructor(private router: Router) {
this.initGlobalRouteEvent();
}
initGlobalRouteEvent() {
this.router.events.subscribe((event: NavigationEvent) => {
if (event instanceof NavigationStart) {
console.log("ionViewWillEnter app-root \n");
return;
}
if (event instanceof NavigationEnd) {
console.log("ionViewDidEnter app-root \n");
}
});
}
}