-2

I want to send an array of data within the .navigate() of angular 8 routing module.

At the home component, the result of:

console.log(this.offers)

Is returning all available offers with all their related data.

After that, I am sending this.offers within the navigate:

this.router.navigate(['/offers'], {queryParams:{ 'offers': this.offers}});

At the offers component, I tried to get the array of offers using:

console.log(this.activatedRouter.snapshot.queryParams.get('offers'))

But the result is just [Object Object].

I tried to use this.activatedRouter.snapshot.params['offers'] but it returned the same thing.

I tried the following:

console.log(this.activatedRouter.snapshot.queryParamMap.get('offers'))

But it returned an [Object Object]:

enter image description here

alim1990
  • 4,656
  • 12
  • 67
  • 130

2 Answers2

1

Instead of using activatedRouter.snapshot, subscribe to this.activatedRoute.queryParams stream which actively emits any changes in query parameters.

@Component({ ... })
export class MyComponent implements OnInit {
  order: string;
  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.route.queryParams
      .filter(params => params.offers)
      .subscribe(params => {
        console.log('Query Params: ', params); // {offers: <value>}
      });
  }
}
Ashish Patel
  • 317
  • 1
  • 8
0

There are several solutions for it according to this link on medium.

The one I need is using this.navigate.

So the solution was at the originating component:

this.router.navigate(['/offers'], { state: {'offers': this.offers, 'info': this.info}});

And at the destination component which is not a child component in my case (it could, no problem with that), we should use the state extras which new from angular 7.2+:

this.offers = window.history.state.offers;

We cannot use the snapshot or queryParams in this case, as according to the link above, it will be removed after ngOnInit() is executed. SO best solution is to use the state extra

alim1990
  • 4,656
  • 12
  • 67
  • 130