i’m using ionic 5 and angular 9. And using angular Router to move between pages. But this leads me to a problem, in ionic the ngOnInit is not called again. I know that there is a NavController in ionic and the lifecycle events tha it throws… But i need to know if there is a way to use angular Router and have the ngOnInit be called again.
extras in ionViewWillEnter are always null (Cannot read property ‘extras’ of null),
In constructor and ngOnInit I receive the data,
so using ionViewWillEnter will not work
At the risk of sounding like a broken record, IMHO lifecycle event handlers should be focused entirely on the internals of a component: setting things up and tearing things down. If there is some fluctuating data source outside the component, subscribe to it and react to changes in it when those changes happen. Don’t involve lifecycle events in that calculation. I would write this like so.
On Page 2 subscribing to the queryParams of the activated Route and reading the state parameter of the navigationExtras and using the ionViewWillEnter instead of ngOnInit
page 2:
constructor(private route: ActivatedRoute,private router: Router) {
this.route.queryParams.subscribe(params => {
if (this.router.getCurrentNavigation().extras.state) {
this.person = this.router.getCurrentNavigation().extras.state as Person;
}
});
}
ionViewWillEnter() {
this.person = this.router.getCurrentNavigation().extras.state as Person;
Sorry that was for other test purpose , forget to delete it ( was doing this to create a scenario as if the user sometimes is connected and other times he needs to connect then go to profile)
I delete that piece of mess , still i got null in ionViewWillEnter
Or if you want you can access the navigation in ngOnInit
Ionic Page Life Cycle
Begin navigation --> ngOnInit --> begin page transition (I think the navigation is finished here that's what cause the problem if i'm not wrong ) --> ionViewwillEnter