0

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

Frans Suny
  • 61
  • 2
  • 1
    its because your `app.component.ts` is not in your life cycle hook. these functions only works on your views/pages. Can you explain why are you using this in `app.component` – Najam Us Saqib Feb 01 '21 at 04:22

2 Answers2

0

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

playerone
  • 987
  • 3
  • 22
  • 44
0

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");
      }
    });
  }
}
Philippe
  • 861
  • 10
  • 10
phiphophe
  • 1
  • 4