Skip to main content

What does UrlTree do?

UrlTree is an object representation of a URL in Angular, not just a string.

It is needed for:

  • building routes programmatically,
  • checking access through guards,
  • navigating without string paths.

Where it's used:

  1. In guards (CanActivate, CanLoad) - you can return a UrlTree to redirect the user:
ts
canActivate(): UrlTree { return this.router.parseUrl('/login'); }
  1. During programmatic navigation:
ts
const tree = this.router.createUrlTree(['/user', 42]); this.router.navigateByUrl(tree);

Summary: UrlTree is a structured way to describe a route, which Angular can read, compare, or apply - like a navigation map, but as an object.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.