简体   繁体   English

角度2-TypeScript(.ts)文件中的routerLinkActive =“ active”等效吗?

[英]Angular 2 - What's the equivalent of routerLinkActive=“active” inside a TypeScript (.ts) file?

I was using routerLinkActive="active" inside the html file with routerLink=[myId] to highlight the active anchor in a ul. 我在带有routerLink=[myId]的html文件中使用routerLinkActive="active"突出显示了ul中的活动锚。

Example: 例:

<a [routerLink]="[myId]" class="list-group-item clearfix" routerLinkActive="active">
  <div class="pull-left">
    <h4 class="list-group-item-heading">{{someName}}</h4>
  </div>
</a>

But when I remove the [routerlink]="[myId]" and replace it with a click listener (that does some calculation and then redirects the route using this.router.navigate(['/someURL', myId]) ) the routerLinkActive="active" no longer works/highlights. 但是,当我删除[routerlink]="[myId]"并将其替换为点击侦听器时(会进行一些计算,然后使用this.router.navigate(['/someURL', myId])重定向路由) routerLinkActive="active"不再起作用/突出显示。

Example: 例:

<a (click)="onClick(myId)" class="list-group-item clearfix" routerLinkActive="active">
  <div class="pull-left">
    <h4 class="list-group-item-heading">{{someName}}</h4>
  </div>
</a>


When inspecting the anchor elements using the routerLink=[myId] , the style is set to: 使用routerLink=[myId]检查锚元素时,样式设置为:

.list-group-item.active, .list-group-item.active:focus, .list-group-item.active:hover {
    z-index: 2;
    color: #fff;
    background-color: #337ab7;
    border-color: #337ab7;
}

Is it possible to set the active anchor style in the .ts file in the onClick() function? 是否可以在onClick()函数的.ts文件中设置活动锚样式? Or is there a simpler way around this? 还是有解决这个问题的更简单方法?

Highlighting a specific link in the navigation is a reflection of the current state of the app: link A is highlighted because the user is looking at page A. 在导航中突出显示特定链接反映了应用程序的当前状态 :链接A被突出显示是因为用户正在查看页面A。

You're asking how to highlight the link as the result of a click . 您在问如何通过单击突出显示链接。 This approach is less than ideal: you could end up highlighting the link before the new route/page is actually activated. 这种方法不太理想:您可能最终会实际激活新路线/页面之前突出显示链接。 Most importantly, if the user reloads the page or navigates to it directly because they bookmarked it, the link will NEVER be highlighted (because no one clicked it). 最重要的是,如果用户由于添加了书签而重新加载该页面或直接导航至该页面,则该链接将永远不会突出显示(因为没有人单击它)。

You need to find a way to highlight the link based on app state, NOT as the result of an action (the typical flow is: action => changes state => updates UI). 您需要找到一种方法来突出显示基于应用程序状态的链接,而不是作为操作的结果(典型流程是:操作=>更改状态=>更新UI)。

Maybe something like this: 也许是这样的:

@Component({
  template: `
    <a [class.active]="currentPath == 'home'">HomeComponent</a>
  `
})
export class HomeComponent {
  currentPath = '';
  constructor(route: ActivatedRoute) {
    // Retrieve current path.
    this.currentPath = snapshot.url.join('');
  }
}

The idea is to retrieve the current path and expose it to the template. 想法是检索当前路径并将其公开给模板。

Like Günter suggested, you can probably find more robust code for testing the current path in routerLinkActive 's source code. 就像Günter建议的那样,您可能会在routerLinkActive的源代码中找到更强大的代码来测试当前路径。

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

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