0

This is my code. Is there any other better or best way to get the queryParams from the url?

  getSolutionId(){
    let solutionId: number;
    this.activatedRoute.queryParams.subscribe((queryParams) => {
      solutionId = queryParams.solutionId ? queryParams.solutionId: null;
    });
    return solutionId;
  }
C.E James
  • 315
  • 1
  • 5
  • 22

4 Answers4

1

You use the router snapshot:

getSolutionId() {
   return this.activatedRoute.snapshot.queryParamMap.solutionId;
}

https://angular.io/api/router/ActivatedRouteSnapshot#queryParamMap

Reactgular
  • 52,335
  • 19
  • 158
  • 208
1

This is already answered by this comment that also references the Angular docs describing how to achieve this.

0

You can do as follows:

this.activatedRoute.queryParams.subscribe((params: Params) => {
      this.param = params['name'];

    }).unsubscribe();
  }

This is case sensitive

CREM
  • 1,929
  • 1
  • 25
  • 35
-1

I am not sure in what terms you are asking to get query params.

Assuming you are trying to create some global method to get queryParams if any. you can call method on every router chang like this -

this.router.events.subscribe(route => {
    this.activatedRoute.queryParams.subscribe((queryParams) => {
      solutionId = queryParams.solutionId || null;
    });
});
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215