【问题标题】:How can I specify query parameters by routerLink directive如何通过 routerLink 指令指定查询参数
【发布时间】:2016-11-15 11:33:38
【问题描述】:

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

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

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

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

<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>

【问题讨论】:

    标签: angular angular2-routing angular2-directives angular2-router angular2-router3


    【解决方案1】:

    你可以这样做

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

    【讨论】:

      【解决方案2】:

      在新的路由组件中,可以这样操作:

      在 URL 中传递参数:

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

      传递e查询参数:

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

      【讨论】:

      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 2012-06-17
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 2015-11-16
      相关资源
      最近更新 更多