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:
- In guards (
CanActivate,CanLoad) - you can return aUrlTreeto redirect the user:
ts
canActivate(): UrlTree {
return this.router.parseUrl('/login');
}- 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 readyPremium
A concise answer to help you respond confidently on this topic during an interview.