How to handle query parameters in angular 2

monty_lennie picture monty_lennie · Jan 4, 2016 · Viewed 115.9k times · Source

In my routable component I have

@RouteConfig {
  {path: '/login',   name: 'Login', component: LoginComponent}
}  

But how do I get the query params if I go to app_url/login?token=1234?

Answer

Jayantha Lal Sirisena picture Jayantha Lal Sirisena · Aug 17, 2016

RouteParams are now deprecated , So here is how to do it in the new router.

this.router.navigate(['/login'],{ queryParams: { token:'1234'}})

And then in the login component you can take the parameter,

constructor(private route: ActivatedRoute) {}
ngOnInit() {
    // Capture the token  if available
    this.sessionId = this.route.queryParams['token']

}

Here is the documentation