December 17, 2023
Can’t bind routerLink to html element a
This is a quick note to a recent confusion I had setting up a route in my Angular application. I had the following link in my HTML template
<a [routerLink]="['/']">Home</a>
The IDE was not posing any issues and my imports for the Component were as follows
imports: [CommonModule, RouterOutlet, RouterModule, HomeComponent,]
However, when I went to use ng serve to build and dev host the site, I kept getting the error
NG8002: Can't bind to 'routerLink' since it isn't a known property of 'a'. [plugin angular-compiler]
I finally figured out, I needed to move the RouterModule to the front of the imports array
imports: [RouterModule, CommonModule, RouterOutlet, HomeComponent,]
Not sure why this made any difference. I was using Linux to build the site, so not sure if this would be similar in Windows or Mac. This was just a head scratcher for me and I wanted to be sure I made note of this just in case I ran across it again in the future.