如何通过routerLink指令指定查询参数 [英] How can I specify query parameters by routerLink directive

查看:97
本文介绍了如何通过routerLink指令指定查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验新的路由器(版本3.0.0-alpha.7),想知道如何通过routerLink指令指定查询参数吗?

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?

下面的Router.navigate()方法生成类似 http://localhost:3000/的URL component-a?x = 1

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

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

但是,我不知道如何使用routerLink指令执行相同的操作.如下所示的模板返回解析器错误...

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>

我能得到的最接近的东西是 http://localhost:3000/component-a; x = 1 ,它使用子路由的语法.

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>

推荐答案

您可以执行以下操作

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

这篇关于如何通过routerLink指令指定查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆