Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify query parameters by routerLink directive

I am experimenting the new router (version 3.0.0-alpha.7) and would like to know how to specify query parameters by routerLink directive?

The Router.navigate() method below generates a URL like http://localhost:3000/component-a?x=1

this.router.navigate(['/component-a'], {queryParams: {x: 1}});

However, I can't figure out how to do the same thing with the routerLink directive. Template like below returns parser error...

<a [routerLink]="['/component-a'], {queryParams: {x: 1}}">Component A</a>

And the closest thing I can get is http://localhost:3000/component-a;x=1, which uses the syntax for child route.

<a [routerLink]="['/component-a', {x:1}]">Component A</a>
like image 949
Yuan Li Avatar asked Jul 13 '16 03:07

Yuan Li


People also ask

How do I get query parameters in Angular 13?

Use queryParamMap to access query parameters. Another way to access query paramters in Angular is to use queryParamMap property of ActivatedRoute class, which returns an observable with a paramMap object.


1 Answers

You can do something like this

<a [routerLink]="['/component-a']" [queryParams]="{x: 1}">Component A</a>
like image 74
Harry Ninh Avatar answered Sep 28 '22 07:09

Harry Ninh