Routing — Angular Technical Interview Questions & Answers

Routing

Routing

  • Router maps URL → component. { path: 'marks', component: MarksPage }. routerLink="/marks" changes the URL without a full reload. <a href="/marks"> reloads and kills in-memory state.
  • router-outlet is where the child page paints. Params: /marks/:roll → ActivatedRoute.snapshot.paramMap.get('roll'). Guard CanActivate blocks /admin if not logged in.
  • Trap: wrong path order — '**' wildcard must be last. Another trap: forgetting RouterModule.forRoot in the root import.
  /login → LoginPage
  /marks → MarksPage
  /admin + CanActivate
  ** → NotFound (last)

Technical Interview Tip: routerLink vs href. One param + one guard line.

Code Examples for Routing

Routing — working example

const routes = [
  { path: "", component: "HomeComponent" },
  { path: "users/:id", component: "UserComponent" },
];
// RouterModule.forRoot(routes)

Routing — routes map URL paths to components; params come from the path.

How to Explain Routing to an Interviewer

routerLink vs href?

routerLink keeps SPA state href reloads

Where does the routed page show?

router-outlet