Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a query string parameter inside an Angular 4 guard?

Tags:

angular

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.

like image 331
vinnylinux Avatar asked Mar 27 '17 19:03

vinnylinux


People also ask

Can we pass object in query params in Angular?

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.

How does Angular handle query params?

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.


1 Answers

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);
}
like image 188
Ontil Avatar answered Oct 11 '22 13:10

Ontil