简体   繁体   English

Angular 2 routerLinkActive

[英]Angular 2 routerLinkActive

So, I understand how to use RouterLink and RouterLinkActive in the traditional sense. 因此,我了解了传统意义上的如何使用RouterLink和RouterLinkActive。 It's pretty easy to define classes that need to be applied but I was curious if you're able to use RouterLinkActive to actually show/hide an element. 定义需要应用的类非常容易,但是我很好奇您是否能够使用RouterLinkActive来实际显示/隐藏元素。

<div class="btn-group btn-group-justified toggle-nav">
  <div class="btn-group">
    <button type="button" role="link" class="btn btn-default btn-lg" routerLink="/log-in" routerLinkActive="btn-primary text-bold">Sign In</button>
  </div>
</div>

I'm looking to show a font awesome icon next to the button text for the selected route (it's a button group). 我希望在所选路线的按钮文本旁边显示一个超棒的字体图标(这是一个按钮组)。 Thank you in advance for the help. 预先感谢您的帮助。 :] :]

You can use the localRef and add classes based on the boolean value of the active state. 您可以使用localRef并根据活动状态的布尔值添加类。 Here is the small snippet for reference. 这是供参考的小片段。

<li routerLinkActive #rla="routerLinkActive">
    <a [routerLink]="['/log-in']">
        Sign In 
        <i *ngIf="rla.isActive" class="fa fa-circle" aria-hidden="true"></i>
    </a>
</li>

When the route is matched., you should see a dark circle in line with the Sign In Text., hope this solves your problem. 匹配路线后,您应该会在“登录文本”中看到一个黑圈,希望这可以解决您的问题。

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

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