Search code examples
angulartypescriptangular-router

how to make parent state active(i.e., add active class) when child is active


I current have some routes

<ul class="">
  <li class="">
    <a [routerLinkActive]="['active']"
       [routerLink]="['/user/traffic/graph']" class="">
       Traffic
    </a>
  </li>
</ul>

Routes

const trafficRoutes: Routes = [
  {
    path: '', component: TrafficComponent, children: [
      { path: '', redirectTo: 'graph' },
      { path: 'graph', component: GraphComponent},
      { path: 'alerts', component: AlertComponent}
    ]
  },
];

The Router Link gets activated when I navigate to GraphComponent but when I navigate to AlertComponent it is not active.

I want to make that router link active. Even when I navigate to AlertComponent

How to achieve that?


Solution

  • My bad. A Rookie Mistake. Changing to the module URL fixes everything

    <ul class="">
      <li class="">
        <a [routerLinkActive]="['active']"
           [routerLink]="['/user/traffic/']" class="">
           Traffic
        </a>
      </li>
    </ul>