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.
queryParams property accepts object as a value. To pass multiple query parameters to router. navigate we can construct an object with list of parametrs and pass it on to queryParams . For example to display books under category fiction and orderby price, use the below code snippet.
import ActivatedRoute from '@angular/router'. Inject ActivatedRoute class in constructor. Access queryParams property of ActivatedRoute class which returns an observable of the query parameters that are available in the current URL route.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With