Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RouterLinkActive on children routes all false

I've the following children routes:

{ path: '', component: LoginSingupComponent,
  children: [
    { path: 'login', component: LoginComponent },
    { path: 'singup', component: SingupComponent },
  ]
},

Navigating to /login or /singup works ok (correct Component is loaded).

This is a excerpt from LoginSingupComponent

<nav md-tab-nav-bar class="mb-1">
  <a md-tab-link routerLink="/login" routerLinkActive [routerLinkActiveOptions]="{exact: true}" #rla="routerLinkActive" [active]="rla.isActive">Entrar {{rla.isActive}}</a>
  <a md-tab-link routerLink="/singup" routerLinkActive [routerLinkActiveOptions]="{exact: true}" #rla="routerLinkActive" [active]="rla.isActive">Criar uma conta{{rla.isActive}}</a>
</nav>

When on /login all rla.isActive == false when on /singup all rla.isActive == true

Tried with and without exact: true

like image 878
Victor Aurélio Avatar asked Oct 20 '17 16:10

Victor Aurélio


People also ask

What is the use of routerLinkActive?

RouterLinkActivelinkTracks whether the linked route of an element is currently active, and allows you to specify one or more CSS classes to add to the element when the linked route is active.

What are child routes?

A child route is like any other route, in that it needs both a path and a component . The one difference is that you place child routes in a children array within the parent route.

How do I know if my routerLink is active?

routerLinkActive is simply an indicator of whether this link represents the active route. @jessepinho - Tried it - [routerLinkActive]="'active'" will only work if you also have [routerLink] in the a-tag. If you had (click)="navitageTo('/route')" instead of [routerLink], and in that function, you called this.


2 Answers

Try like this :

<nav md-tab-nav-bar class="mb-1">
    <a md-tab-link [routerLink]="['/']" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">Home</a>
    <a md-tab-link [routerLink]="['/login']" routerLinkActive="active">Login</a>
    <a md-tab-link [routerLink]="['/singup']" routerLinkActive="active">Signup</a>
</nav>
like image 146
Chandru Avatar answered Oct 07 '22 16:10

Chandru


Found the problem.

It's was because i was exporting both rounterLinkActive with same variable name which causes this weired behavior.

<a md-tab-link routerLink="/auth/login" routerLinkActive [active]="rlal.isActive" #rlal="routerLinkActive">Entrar {{rlal.isActive}}</a>
<a md-tab-link routerLink="/auth/singup" routerLinkActive [active]="rlas.isActive" #rlas="routerLinkActive">Criar uma conta{{rlas.isActive}}</a>

Solves the problem.

like image 42
Victor Aurélio Avatar answered Oct 07 '22 16:10

Victor Aurélio