繁体   English   中英

使用 skipLocationChange 导航时 Angular routerLinkActive 不工作

[英]Angular routerLinkActive not working when navigating using skipLocationChange

假设我有以下路线:

const routes: Routes[] = [
  {
    path: 'a',
    component: AComponent
  },
  {
    path: 'b',
    component: BComponent
  }
]

在这样的路径上,我a一些与routerLinkActive类的 Navbar 链接:

<li
  [routerLinkActiveOptions]="{exact: true}"
  routerLinkActive="is-active"
  class="BottomBar-menuItem"
>
  <a routerLink="../a">A</a>
</li>

当我使用skipLocationChange从路径a导航到路径b时出现问题

//component A

navigate() {
 this.router.navigate(['../b'], { skipLocationChange: true, relativeTo: this.activatedRoute })
}

浏览器 url 没有改变,它仍然在/a上,视图更改为组件 B,但routerLinkActive不应用is-active类。

routerLinkActive应该仍然应用is-active类,因为 url 不会改变,对吗? 我在这里错过了什么?

发生这种情况是因为您使用了“ skipLocationChange: true ”并跳过了 URL 中的位置更改,您可以在以下位置看到一个示例:

https://stackblitz.com/edit/angular-skiplocationchange

和角度文档: https ://angular.io/api/router/NavigationExtras

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM