FrankTheDevop FrankTheDevop FrankTheDevop FrankTheDevop FrankTheDevop

Angular Tips I: Fix Can’t bind to ‘routerLink’ since it isn’t a known property of ‘a’.

150 150 Frank

Hi everyone,

this will be a quicky. I guess everyone has had this error and was so used to it working that you forgot how to solve it. Because I just had that experience I write this post.

What you tried to achieve

Add a routerLink entry to an a html element in the Angular template.

Error

Can’t bind to ‘routerLink’ since it isn’t a known property of ‘a’.

Solution

Remember to import the RouterModule in every Module where you want to user routerLink.
To do that add import { RouterModule } from '@angular/router'; at the top of the corresponding module. And don´t forget to add it to the imports array:

@NgModule({
imports: [ CommonModule, RouterModule ],
...
})
export class ...

If you put that into a shared Module don´t forget the exports array too:

exports: [
...,
...,
RouterModule
]

I hope this helps you avoid a headache when you see the message “Can’t bind to ‘routerLink’ since it isn’t a known property of ‘a’.” again.

Yours sincerely,
Frank

Sources:
(1) https://blog.ng-book.com/basic-routing-in-angular-2/
(2) https://coryrylan.com/blog/introduction-to-angular-routing
(3) https://toddmotto.com/angular-component-router
(4) https://malcoded.com/posts/angular-fundamentals-routing

Leave a Reply

Your email address will not be published.

eight − 8 =