Build Angular Router navigation: first-match routes, canMatch, guards with UrlTree redirects and resolvers

hardAbout 70 minutesUpdated CodingProangularangular-routerroutingnavigation-guardsasyncrace-conditions
Not solved

Share this question

Problem

Angular's router turns a URL into a chain of activated routes, and every step of a navigation can be asynchronous: canMatch while matching, canDeactivate, canActivateChild and canActivate guards, then resolvers. Interviewers ask about the order of these steps, what happens when a guard returns a UrlTree, and what happens to a slow navigation when the user clicks somewhere else. Build that navigation pipeline.

let router;
router = createRouter([
  { path: "login", component: Login },
  { path: "orders", component: Orders, canActivate: [() => auth.loggedIn || router.parseUrl("/login")] },
  { path: "**", component: NotFound },
]);
await router.navigate("/orders"); // true, but router.url === "/login"
router.events.map((e) => e.type); // NavigationStart, NavigationCancel, NavigationStart, NavigationEnd

Ref. UR-1453 · © UIRound · uiround.com/questions/build-angular-router-navigation-first-match-routes-canmatch-guards-with-urltree-redirects-and-resolvers

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment