can't bind to 'routerLink' since it isn't a known property

How to fix can’t bind to ‘routerLink’ since it isn’t a known property!

In the last article, we solved the router outlet problem. Another error related to routing may appear which is can’t bind to ‘routerLink’ since it isn’t a known property.

can't bind to 'routerLink' since it isn't a known property

What is routerLink in Angular?

The RouterModule of Angular is a big Module that contains many built-in directives. One of them is routerLink which is responsible for replacing href to navigate from the HTML code.

let’s see how to use the routerLink and what is the difference between href and routerLink ?

in the first example we will work with href let’s see the results :

Suppose that we have this code navbar in our app component HTML. When clicking on Home or Products, the page will refresh and then redirect us to the wanted Page. Of course we don’t need that behavior. We will lose app state.

for that reason, Angular Helped us with routerlink Directive.

let’s use the same example, we will just replace the href with routerLink :

And guess what now, We are able to navigate easily without refreshing the page. We can keep our app state.

can’t bind to ‘routerLink’ since it isn’t a known property

This error may happen in the case of not importing the RouterModule into the module file you’re using the routerLink in. Keep in mind to declare or import RouterModule once, either directly in your module file, or by declaring it in your routing file and then exporting it. Personally, I prefer the second.

let’s see the two Solutions.

Solution 1

Solution 2

Conclusion

We wanted to continue working with the routerModule by covering the can’t bind to routerLink since it isn’t a known property error, and the solution is quite simple, just Adding the RouterModule to app Module or the needed Module ( depending on lazy loading) .

In the last article, we solved the router outlet problem. Another error related to routing may appear which is can’t bind to ‘routerLink’ since it isn’t a known property. What is routerLink in Angular? The RouterModule of Angular is a big Module that contains many built-in directives. One of them…

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest