Router — Angular Technical Interview Questions & Answers

Router

Router

  • Router is the same idea as routing, said from the service side. Inject Router and call this.router.navigate(['/marks', roll]). After login, navigate. After logout, navigate to /login.
  • ActivatedRoute is the current page. Router is the taxi. Don’t inject ActivatedRoute when you only need to navigate.
  • Trap: navigateByUrl('/marks?x=1') vs navigate with queryParams — know which one you used. Another trap: navigation inside a constructor before the view exists.

Technical Interview Tip: navigate(['/marks', roll]) + why not constructor.

Code Examples for Router

Router — working example

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

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

How to Explain Router to an Interviewer

Router vs ActivatedRoute?

Router moves ActivatedRoute reads params