【问题标题】:routerLinkActive matching routes with different query parametersrouterLinkActive 匹配不同查询参数的路由
【发布时间】:2026-02-22 03:55:02
【问题描述】:

这段代码会生成一些链接,例如

/报告

/reports?collection=foo

/reports?collection=bar

所有链接都显示为活动的。如何仅将一个确切的链接显示为活动的?

<li [routerLinkActive]="['link-active']" [routerLinkActiveOptions]="{exact: true}">
  <a [routerLink]="['/reports']">
    <span class='glyphicon glyphicon-expand'></span> Reports
  </a>
</li>
<li [routerLinkActive]="['link-active']" [routerLinkActiveOptions]="{exact: true}" *ngFor="let collection of collections">
  <a [routerLink]="['/reports']" [queryParams]="{collection: collection}">
    <span class='glyphicon glyphicon-unchecked'></span><span>{{collection}}</span>
  </a>
</li>

【问题讨论】:

    标签: angular angularjs-routing


    【解决方案1】:

    在指定路由配置和匹配路由时不包括查询和可选参数。这就是为什么 exact: true 不适合你。

    如果您需要确保对路由进行不同的处理,请尝试改用必需的参数。然后在配置中定义路由的细节,并在路由器匹配路由时考虑。

    【讨论】: