Execute same function before each page enters


#1

Hello Everyone .

I have pages in my app and on each page, i have ionicviewdidload in which i have my api calls written.what i want now is before ionicviewdidload happens,i want to execute one api call and after that i want ionicviewdidload to happen. thing is i want this for all pages. before their ionicviewdidload happens,i want the same function to be executed before that

vue.js has a nice way. router.beforeEach(){ code } i want the same thing.

any help?

thanks in advance


#2

have you tried

export class Classname implements OnInit {

ngOnInit() {
}

}

https://stackoverflow.com/questions/43703271/ngoninit-vs-ionviewdidload-in-ionic-2


#3

I know that. whatever i write in ngOnInit,it will be called. but i want the same function to be executed in every page.if i follow your advice,i have to write this same function in every page’s ngoninit which is a bad practice


#4

Perhaps adding it to your root nav page ionViewWillLeave?

Or using Events in some creative fashion

I don’t know that calling your event on every page is necessarily bad practice, especially considering the fact that it may be necessary.


#5

I don’t think that’s possible. You could subscribe to NavController.viewDidLoad() and do something each time a page is loaded. This isn’t as clean as Vue. Though I don’t know whether that aspect of Vue would be super clean inside Ionic, because Ionic’s lazy loading is page-based instead of module-based.