Angular routerLink does not navigate to the corresponding component Angular routerLink does not navigate to the corresponding component angular angular

Angular routerLink does not navigate to the corresponding component


The code you are showing there is absolutely correct.

I suspect that your problem is that you are not importing RouterModule (which is where RouterLink is declared) into the module which uses this template.

I had a similar problem and it took me some time to solve as this step is not mentioned in the documentation.

So go to the module that declares the component with this template and add:

import { RouterModule } from '@angular/router';

then add it to your modules imports e.g.

@NgModule({  imports: [    CommonModule,    RouterModule  ],  declarations: [MyTemplatesComponent]})export class MyTemplatesModule { }

Along with having the correct import statements, you'll also need a place for that routerLink to be shown, which is in the <router-outlet></router-outlet> element, so that also needs to be placed somewhere in your HTML markup so the router knows where to display that data.


don't forget this to add this below in your template:

<router-outlet></router-outlet>


Try changing the links as below:

  <ul class="nav navbar-nav item">    <li>        <a [routerLink]="['/home']" routerLinkActive="active">Home</a>    </li>    <li>        <a [routerLink]="['/about']" routerLinkActive="active">About this</a>    </li>  </ul>

Also, add the following in the header of index.html:

<base href="/">