简体   繁体   English

routerLinkActive 不适用于 router.navigateByUrl 或导航

[英]routerLinkActive does not work with router.navigateByUrl or navigate

I am changing the route through a (click) and not a [routerLink] and this is not add class active but change the route我正在通过(click)而不是[routerLink]更改路线,这不是添加类活动而是更改路线

I have to generate an id in the server and after that run this.router.navigate and activate the link in my side menu我必须在服务器中生成一个 id,然后运行this.router.navigate并激活我侧菜单中的链接

    <li
      routerLinkActive="active" 
      [routerLinkActiveOptions]="{exact: false}">
      <a
        class="nav-link"
        (click)="addNew()"
      >
      </a>
    </li>

my function我的功能

  addNew(): void {
    this.userService.create().subscribe(res => {
      this.router.navigateByUrl(`user/edit/${res.data.id}`);
    });
  }

Other places in the menu are using [routerLink] and it works normally菜单中的其他地方都在使用[routerLink],它工作正常

thanks for help感谢帮助

routerLinkActive relies on having a routerLink on either the same host or a descendant in order to know what the candidate links are. routerLinkActive依赖于在同一主机或后代上有一个routerLink来知道候选链接是什么。

You need to add a routerLink to your <a> .您需要将routerLink添加到您的<a>

<li routerLinkActive="active" [routerLinkActiveOptions]="{exact: false}">
  <a routerLink="???" class="nav-link" (click)="addNew()">
  </a>
</li>

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

相关问题 Angular中router.navigateByUrl和router.navigate的使用方法 - How to use router.navigateByUrl and router.navigate in Angular router.navigateByUrl()导致导航到根并重新加载,角度为2.4.7 - router.navigateByUrl() causes navigation to root and reload, angular 2.4.7 将router.navigateByUrl之后的组件刷新为具有不同参数的相同URL - Refresh component after router.navigateByUrl to the same URL with different parameters router.navigateByUrl不发送值Angular - router.navigateByUrl don't send value Angular 如何在使用 Router.navigateByUrl 时将数据传递给组件 - How to pass data to a component while using Router.navigateByUrl 如果页面仍在加载,Angular router.navigateByUrl()无法正常工作 - Angular router.navigateByUrl() not working if page is still loading 如何使用 Karma 和 Jasmine 在 Angular 中模拟 router.navigateByUrl(...).then - How to mock router.navigateByUrl(…).then in Angular with Karma and Jasmine CanDeactive Guard:通过 router.navigateByUrl() 路由时如何重置活动 class - CanDeactive Guard: How to reset active class when routing by router.navigateByUrl() RouterLinkActive在ngFor中工作 - RouterLinkActive does work in ngFor 角度路由错误 - this.router.navigateByUrl() 和 this.router.navigate() - Angular routing bug - this.router.navigateByUrl() and this.router.navigate()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM