I send data between pages like this:
this.navCtrl.navigateForward(['artist-info-page'], {
state: {
fullArtist: item,
simpleSearch: true
}
})
Most of the times this triggers the constructor of 'artist-info-page', where I read the state like this:
this.artistInfoParams.fullArtist = this.router.getCurrentNavigation().extras.state.fullArtist
But sometimes navigateForward doesn't trigger the constructor, so I can't retrieve the parameters. It simply calls ionViewWillEnter, where from my understanding I have no access to the router's navigation state.
I also tried moving to router.navigate instead of using NavController, I switched for one page and the behavior is the same. I haven't switched my entire project as I use NavController a lot and it doesn't seem to help.
Am I doing something wrong? Or is this a bug and if so, do you know any workaround?
I decided to solve this by defining my own service for data transfer between pages. I based my decision on this great link. I implemented something simpler for now, as it was enough for me - a simple class with only one parameter called state:
export class DataServiceProvider {
public state: any;
}
Then I simply fill the state parameter before navigating to the page:
var localState = {
fullArtist: item,
simpleSearch: true
};
this.dataService.state = localState;
this.navCtrl.navigateForward('artist-info-page');
And retrieve it in the page:
ionViewWillEnter() {
var localState = this.dataService.state;
}
Of course, there's space for some improvement in the future, for example making sure the state is not preserved from an older page transition (when the current state needs no state).