routerLink is not working in Angular 5

If the routerLink is not working in your Angular 2/4/5 application, here are some common mistakes and solutions.

  • Check if you have added RouterModule in the imports of Module where you created this component.

For example, if you have created a NavComponent in NavigationModule and imported in your HomeModule and using it. Then you have to import ReportModule in your NavigationModule.

Like –

import { RouterModule } from '@angular/router';
NgModule({
  imports: [
    CommonModule,
    RouterModule
],
declarations: [NavComponent],
exports: [NavComponent]
})
export class NavigationModule { }
  • If you have any computations in the routerLink, like appending a value dynamically, enclose routerLink with square brackets
<a [routerLink]="['/car/', carModelName].concat(details)"></a>

Here, carModelName and details can be class variables.
Hope this helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s