Angular 2 routerLinkActive

夙愿已清 提交于 2020-01-06 05:00:08

问题


So, I understand how to use RouterLink and RouterLinkActive in the traditional sense. 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.

<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. :]


回答1:


You can use the localRef and add classes based on the boolean value of the active state. 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.



来源:https://stackoverflow.com/questions/44576766/angular-2-routerlinkactive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!