Performance: RouterLink for navigation
This affects page load speed and interaction responsiveness by controlling how navigation updates the URL and renders new views without full page reloads.
<a routerLink="/dashboard">Dashboard</a><a href="/dashboard">Dashboard</a>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using <a href> for navigation | Full DOM reload | Multiple reflows | High paint cost | [X] Bad |
| Using <a routerLink> for navigation | Partial DOM update | Single reflow | Low paint cost | [OK] Good |
RouterLink in Angular?RouterLink is used to create links that change the URL and navigate inside the Angular app smoothly.RouterLink in a template?routerLink="/path" without square brackets.<a routerLink="['/home']">Home</a> (A) uses array literal as static string, leading to malformed path. <a [routerLink]="/home">Home</a> (B) has invalid binding expression (missing string quotes). <a routerLink=['/home']>Home</a> (C) has wrong brackets syntax without quotes. <a routerLink="/home">Home</a> (D) uses correct static syntax.<a [routerLink]="['/profile', userId]">Profile</a>
userId = 42, what URL will the link navigate to when clicked?[routerLink]="['/profile', userId]" creates a URL by joining segments, so it appends userId as a path segment.userId = 42, the URL becomes /profile/42.<a [routerLink]="'/dashboard'">Dashboard</a>
routerLink="/dashboard".RouterLink in the template to handle this condition?<a [routerLink]="userId ? ['/user', userId] : ['/user']">User Details</a> uses a ternary operator inside the binding to choose the path based on userId validity, which is clean and safe.<a *ngIf="userId" [routerLink]="['/user', userId]">User Details</a> hides the link if userId is falsy, which may be okay but removes the link entirely. <a [routerLink]="['/user', userId || 'default']">User Details</a> uses a fallback string which might not be desired. <a [routerLink]="/user/{{userId}}">User Details</a> uses interpolation inside the binding expression, which is invalid template syntax.Pass the test and this pattern moves unknown