Angular 中具有多个参数的 RouterLink

RouterLink with multiple params in Angular

我想为具有多个参数的路由创建一个 link 并将它们绑定到 tempalte 中。到目前为止,我一直在通过在 (click) 事件上执行函数来执行此操作,但我想知道在 RouterLink 的绑定中是否可行。

这是我用来绑定参数的函数:

redirect() {
    this._router.navigate( ['/category', { cat: this.category, page: this.page }]);
}

我的路线如下:

{
    path: 'category/:cat/:page',
    component: PostComponent
}

我可以在 routerLink 指令中做同样的事情吗?

是的,当然,您可以使用 routerLink 动态形成 href 标签以进行导航。数组中的值,其中每个值将根据组件上下文进行评估。

[routerLink]="['/category', category, page ]"

你可以这样做,例如:-

<p *ngFor="let emp of employees; let i = index">
  <li><a [routerLink]="['delete', i]">{{emp.name}}</a>({{emp.status}})</li>
</p>

希望对您有所帮助,

谢谢