I have an app build with Angular 8. Routerlinkactive
is not activated on dynamically generated route at the beginning, but when I click on some other links in the same module and come back then it works.
The routerLinkActive
is set in the UserComponent
, and it should activate the anchor in navigation when the child (UserProfileComponent
) is loaded based on url params.
I tried many solutions such as usage of template variable, for example
[routerLinkActive]="['']" [ngClass]="rla.isActive ? 'mat-accent' : 'mat-primary'" #rla="routerLinkActive"
or applied [routerLinkActiveOptions]="{exact:true}"
but none of them were helpfull.
This is last version of my HTML code.
<ul role="list" class="list-horizontal">
<li>
<a class="mat-primary"
routerLinkActive="mat-accent"
[routerLink]="'/user/profile/' + profile?._id"
mat-raised-button>Profile </a>
</li>
<li>
<a class="mat-primary"
[routerLink]="'/user/posts/'"
[routerLinkActive]="['mat-accent']"
mat-raised-button>Posts</a>
</li>
<li>
<a class="mat-primary"
[routerLink]="'/user/favorite-posts/'"
[routerLinkActive]="['mat-accent']"
mat-raised-button>Favourites</a>
</li>
</ul>
I guess the problem is in dynamically generated route ( which is based on user id ),
For example http://my-domain/user/profile/5dd58c41409cccea2c9296f3
because when I access other components directly, for instance http://my-domain/user/posts
then routerLinkActive
works fine sets active
class to the corresponding link.
This is how my routes are configured for current module.
const routes: Routes = [{
path: '',
component: UserComponent,
children: [
{
path: 'profile/:id',
component: UserProfileComponent
},
{
path: 'posts',
component: UserPostsComponent
},
{
path: 'favorite-posts',
component: UserFavoritePostsComponent
}
]
}];
Any hint is appreciated. Thanks!