In your example, what happens when you go back a page or pages? Are the params not then lost?
I am not committed yet to any particular way to achieve this but this is what I am working with.
I have a NavigationService with a couple of arrays that I save to storage, one being session_routes.
I have a function I call from other pages when I go to navigate around the app.
async addRoute(routeObj) {
this.session_routes.push(routeObj);
this.storage.set('session_routes', JSON.stringify(this.session_routes));
await this.navCtrl.goForward(routeObj.url);
}
I call this like so:
this.NavigationService.addRoute({url: '/item/' + '1094' +'?n='+this.NavigationService.session_routes.length, params: {anything: "somecrazydata"}});
In the receiving page I have:
if (this.platform.getQueryParam("n")) {
this.NavigationService.session_routes[this.platform.getQueryParam("n")];
}
The URL param ānā is what I wanted to get rid of in this thread. I wanted it to be an option in the app-route params, so that static links did not need to pass it. I also do not like the look of it as this is mainly a PWA.
I welcome thoughts on this approach.
My other approach I am considering is saving extra param data in the ItemService itself.
In ItemService, I have all the Items in an array. As each is loaded in ItemPage ( via navCtrl or url ), any extra param data is saved to the Item object in the array. So, whenever it is reloaded via GoBack or direct link, the last loaded param data is still there.
This will eliminate the need for using the URLparam ān=ā ( or similar ) but now I am dealing with this across multiple Services.
@reedrichards this would be like your example but for multiple Services. Not ideal.
So, it looks like now I am going to try and combine the twoā¦ using the above NavigationService to catch navigations and then push the extra data to the Item object in the Items array in the ItemsService.
And it looks like this:
NavigationService:
async addRoute(routeObj) {
this.session_routes.push(routeObj);
this.storage.set('session_routes', JSON.stringify(this.session_routes));
let item_id = routeObj.params.item_id
let item = this.ItemsService.items_ar.filter(item => item.item_id == item_id)[0];
if(item){
item.navParams = routeObj.params;
}
await this.navCtrl.goForward(routeObj.url);
}
ItemPage:
this.item_id = this.route.snapshot.paramMap.get('item_id');
this.item = this.ItemsService.items_ar.filter(item => item.item_id == this.item_id)[0];
console.log("MY ITEM NAVPARAMS: "+JSON.stringify(this.item.navParams));
Sending Page:
this.NavigationService.addRoute({go: true, url: '/item/' + '1094', params: {item_id: "1094", nav_id: this.NavigationService.session_routes.length, extra: "some crazy data"}});
This could use refinement but it accomplishes multiple things:
- Sends extra paramData
- Solves optional app-route param problem
- Eliminates extra url param ān=ā etcā¦
- Works for static links
- Saves historical paramData
Overkill - likely.