Angular 6 - How do I set a Parent Menu Item To Active using routerLinkActive When Clicking on Its Child Menu Item?

强颜欢笑 提交于 2019-12-06 07:11:45

So when I compared my code to https://stackblitz.com/edit/parent-child-active?file=src%2Fapp%2Fapp.component.css this.

The only difference I could find is that the routerLinks and routerLinkActive attributes that I was set to the a tags.

So i decided to to structure it like this instead:

<ul>
  <li routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
    <a href="#" routerLink="/">Home</a>
  </li>
  <li  routerLinkActive="active">
    <a href="#" routerLink="/parent">Parent Component</a>
    <ul>
      <li routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
        <a href="#" routerLink="/parent/child">Child Component</a>
      </li>
    </ul>
  </li>

</ul>

If you want the parent class to remain active while the child class is too, you need to set the routerLink for the child as /parent/child. If the beginning of the active routerLink matches exactly another one, both will become active:

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