Performance: Why routing is needed for SPAs
Routing in SPAs affects page load speed and interaction responsiveness by managing content changes without full page reloads.
this.router.navigate(['/about']);
window.location.href = '/about';
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full page reload navigation | High (reloads entire DOM) | Multiple (full page) | High (full repaint) | [X] Bad |
| Angular router navigation | Low (component swap) | Single or none | Low (partial repaint) | [OK] Good |
path and component keys.{ path: 'home', component: HomeComponent } uses path and component correctly.const routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];/about?/about matches the route with path: 'about'.path: 'about' uses AboutComponent.const routes = [
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}<a routerLink='/about'>About</a>.AppRoutingModule is not imported in AppModule, routing won't activate, causing navigation failure.path: '**' as a wildcard to catch all unmatched URLs.{ path: '**', component: NotFoundComponent } shows the Not Found page for unknown URLs.Pass the test and this pattern moves unknown