routerlink - Angular 2/4 - routerLinkActive not working properly -


i have problem routerlinkactive.

here 2 gifs explain.

  1. first problem: when start app, none of routerlinkactive give class active. if click on different route, finaly works.

enter image description here

  1. when click @ first on current route, class isn't displayed.

enter image description here

here code:

<ul class="nav">    <li *ngfor="let menuitem of menuitems" routerlinkactive="active" class="{{menuitem.class}}">        <a [routerlink]="[menuitem.path]">            <i class="material-icons">{{menuitem.icon}}</i>            <p>{{menuitem.title}}</p>        </a>    </li> </ul> 

here tree of route. (in red component called)

enter image description here

and route code:

import ...  const routes : routes = [   {     path: '',     component: homecomponent,     canactivate: [authguard],     children: [       {         path: 'dashboard',         component: dashboardcomponent       }, ..., {         path: 'challenges',         component: imagerankcomponent       }, {         path: 'niveau',         component: levelcomponent       }, {         path: '',         redirectto: 'dashboard',         pathmatch: 'full'       }     ]   } ];  @ngmodule({   imports: [routermodule.forchild(routes)],   providers: [     {       provide: locationstrategy,       useclass: hashlocationstrategy     }   ],   exports: [routermodule] }) export class homeroutingmodule {} 

and menuitem is:

this.menuitems = routes.filter(menuitem => menuitem); const routes : routeinfo[] = [{     path: 'dashboard',     title: 'dashboard',     icon: 'dashboard',     class: '' }, {     path: 'challenges',     title: 'tous les challenges',     icon: 'dashboard',     class: '' }, {     path: 'niveau',     title: 'niveau en ligne',     icon: 'dashboard',     class: '' }] 

do know can problem?

edit:

i have tried:

absolute route. ie:

 path: '/home/dashboard' 

with

<a [routerlink]="menuitem.path"> 

and

<a [routerlink]="[menuitem.path]"> 

and result same. not working.

edit2:

adding:

[routerlinkactiveoptions]="{exact: true}" to:

<li *ngfor="let menuitem of menuitems" routerlinkactive="active" class="{{menuitem.class}} " [routerlinkactiveoptions]="{exact: true}"> 

doesnt resolve problem.

edit3:

the extension augury says me routerlink true route. class isn't activated in dom.

enter image description here

enter image description here

edit4:

so, have done exploration.

i have found if put menucomponent (sidebar) in parent root, working, active class displayed (but don't want put in parent)

moving menu parent works.

edit5:

i have done plunker of situation... , plunker works... dont it.

https://plnkr.co/edit/7kmly2

try :

<li routerlinkactive="active" [routerlinkactiveoptions]="{exact: true}">     <a>home</a> </li> 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -