vinnylinux
vinnylinux

Reputation: 7024

Reading a query string parameter inside an Angular 4 guard?

I'm trying to read a query string parameter inside the canActivate method of an Angular guard. Unfortunately, the queryParams parameter from both ActivatedRouteSnapshot and RouterStateSnapshot are empty, even though the query string exists.

Example: bla.com/?uid=123

That query string parameter can be sent to any URL and i want my guard to be able to look at it.

Upvotes: 18

Views: 14996

Answers (2)

Jnr
Jnr

Reputation: 1654

Or if you are using parameter in your route:

canActivate(route: ActivatedRouteSnapshot) {
  console.log("Id:", route.params['uid']);
}

Upvotes: 3

Ontil
Ontil

Reputation: 366

Example url: http://example.com/oper/sale?code=you_code_here

import { ActivatedRouteSnapshot } from "@angular/router";
...

canActivate(route: ActivatedRouteSnapshot) {
    console.log("code", route.queryParams.code);
}

Upvotes: 35

Related Questions