I have a situation where I am sending data through routing and fetching it. Here's the code snippet for sending the data:
navigateWithState(x) {
console.log(x);
this.router.navigateByUrl('/full-layout/add-form/1', { queryParams: {x} });
}
And here is the code snippet for fetching the data:
constructor(private route: ActivatedRoute) {
if(this.options == 1){
console.log('edit');
this.route
.queryParams
.subscribe(params => {
console.log(params);
});
}
else{
console.log('add');
}
}
Despite having the data in the console, when passing data through the router, it's showing up as an empty array. Here's the console screenshot: https://i.sstatic.net/EOeBa.png
I tried using navigationExtras like this:
navigateWithState(x) {
console.log(x);
const queryParams: any = {};
queryParams.myArray = JSON.stringify(x);
const navigationExtras: NavigationExtras = {
queryParams
};
this.router.navigateByUrl('/full-layout/add-form/1', navigationExtras);
Then, in the .ts file, I tried fetching the data like this:
if(this.options == 1){
console.log('edit');
const myArray = this.route.snapshot.queryParamMap.get('myArray');
console.log(myArray);
}
else{
console.log('add');
}
But it's showing null in myArray instead of the actual data.