I am busy with an upgrade of an old IONIC app to IONIC 4 and things are going well, however I am not able to find anything that details how to pass data and use pages in the routing of Angular.
Here is a code snippet of the problem:
OLD CODE
this.navCtrl.setRoot(VendorBookingDashboardProgressPage, { booking: temp });
In the above code, you will see that booking: temp is passed as a parameter for additional data and VendorBookingDashboardProgressPage is the reference page to navigate to.
NEW CODE
this.navCtrl.navigateRoot('vendor/vendor-booking-dashboard-progress'); -- This is where the booking: temp parameter is missing and must be included as well as hard-coded URL where page reference should rather be included but a string parameter is required.
I can live with hard-coding the URL, but the parameters are key and need to know how this can be achieved please.
Try doing it using navigation extras like this:
import { NavigationExtras } from '@angular/router';
import { NavController } from '@ionic/angular';
constructor( public navCtrl: NavController) {
}
//this should be within a function
const navigationExtras: NavigationExtras = {
queryParams: {
special: JSON.stringify(temp)
}
};
this.navCtrl.navigateRoot(['vendor/vendor-booking-dashboard-progress'],
navigationExtras);
Then to retrieve it from the page routed to, try this within the page's ts file:
import { ActivatedRoute, Router } from '@angular/router';
data: any;
constructor(
public activatedRoute: ActivatedRoute,
private router: Router) {
}
ngOnInit(){
this.activatedRoute.queryParams.subscribe(params => {
if (params && params.special) {
//store the temp in data
this.data = JSON.parse(params.special);
}
}
}
You can try below method to pass data between pages
let navigationExtras: NavigationExtras = {
queryParams: {
special: JSON.stringify(this.user)
}
};
this.router.navigate(['details'], navigationExtras);
setData(id, data) {
this.data[id] = data;
}
getData(id) {
return this.data[id];
}
let navigationExtras: NavigationExtras = {
state: {
user: this.user
}
};
this.router.navigate(['details'], navigationExtras);
Ref : https://ionicacademy.com/pass-data-angular-router-ionic-4/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With