如何通过 routerLink 指令指定查询参数

How can I specify query parameters by routerLink directive

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

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

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

但是,我不知道如何用 routerLink 指令做同样的事情。模板如下 returns 解析器错误...

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

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

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

你可以这样做

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

在新的路由器组件中,你可以这样做:

在URL中传递一个参数:

<a [routerLink]="['/component-a', 1]">Component A</a>

传递 e 查询参数:

<a [routerLink]="['/component-a', { x: 1 }]">Crisis Center</a>