How to fix Angular standalone component: Can't bind to 'routerLink' since it isn't a known property of 'a'

Problem:

When opening your Angular app with a standalone component, you see the following error message:

Can't bind to 'routerLink' since it isn't a known property of 'a'

Solution

In the component where the error occurs, add

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

and add

RouterModule,

to the imports of the component. Example:

@Component({
  selector: 'app-top-bar',
  standalone: true,
  imports: [
    CommonModule,
    RouterModule
  ],
  templateUrl: './top-bar.component.html',
  styleUrl: './top-bar.component.sass'
})
export class TopBarComponent {

}