Sub topic 12
Inspecting the router tree
This chapter assumes you know how to define routes with provideRouter, and that you have used the Components tab from earlier chapters. It also assumes Angular DevTools is installed and your app runs in development mode.
The Router tree is a panel in Angular DevTools that draws your route configuration as a tree. You can see which routes exist, which one is active, and what each one is wired to. It answers the question "why did this URL render this component, or nothing?" without reading the route files by hand.
What the Router tree shows
Open DevTools, then open the Angular tab. If the app uses the Angular router, a Router tree tab appears next to Components and Profiler. If the tab is missing, check three things:
- The page is a development build.
- Your Angular and Angular DevTools versions are both recent. The panel was added after the Components and Profiler tabs, so older versions lack it.
- The app actually calls
provideRouter(or importsRouterModule.forRoot).
The panel renders a node for every route in the configuration. Each node shows the route's path segment and the component it renders. The route or routes that match the current URL are highlighted. Selecting a node shows its details, such as its path and the component it loads. The exact fields depend on the DevTools version.
A toggle for showing full paths changes what each node displays. Without it, a node shows only its own segment, such as reports. With it, the node shows the combined path from the root, such as /admin/reports. Turn it on when you need to compare a node against a URL in the address bar.
A sample configuration
The following routes give the panel something worth inspecting. The configuration has an eager route, a redirect, a guarded lazy section, a title, route data, and a wildcard.
// app.routes.ts
import { Component, inject } from '@angular/core';
import { CanActivateFn, Router, Routes } from '@angular/router';
@Component({ standalone: true, template: '<h2>Home</h2>' })
export class HomeComponent {}
@Component({ standalone: true, template: '<h2>Login</h2>' })
export class LoginComponent {}
@Component({ standalone: true, template: '<h2>Not found</h2>' })
export class NotFoundComponent {}
export const authGuard: CanActivateFn = () => {
const router = inject(Router);
const signedIn = localStorage.getItem('token') !== null;
return signedIn ? true : router.parseUrl('/login');
};
export const routes: Routes = [
{ path: '', pathMatch: 'full', redirectTo: 'home' },
{ path: 'home', component: HomeComponent, title: 'Home' },
{ path: 'login', component: LoginComponent, title: 'Login' },
{
path: 'admin',
canActivate: [authGuard],
loadChildren: () => import('./admin/admin.routes').then((m) => m.ADMIN_ROUTES),
},
{ path: '**', component: NotFoundComponent },
];// admin/admin.routes.ts
import { Component } from '@angular/core';
import { Routes } from '@angular/router';
@Component({ standalone: true, template: '<h2>Admin dashboard</h2>' })
export class DashboardComponent {}
@Component({ standalone: true, template: '<h2>Reports</h2>' })
export class ReportsComponent {}
export const ADMIN_ROUTES: Routes = [
{ path: '', component: DashboardComponent },
{ path: 'reports', component: ReportsComponent, data: { section: 'reports' } },
];Register the routes in app.config.ts with provideRouter(routes). The root component needs a <router-outlet /> in its template, and the root must import RouterOutlet.
Reading the tree
The diagram shows the tree you should expect for the sample configuration after the admin section has been loaded.
Three points are easy to miss.
First, the redirect route (path: '' with redirectTo: 'home') has no component. Redirects are configuration, not rendered views. Do not expect a node with a component for it, and do not treat its absence from the Components tab as a bug.
Second, the lazy admin section has no children until the router loads its chunk. If you open the Router tree on the home page, the admin node can appear without its children. Navigate to /admin once, then look again. This is the most common reason people think a child route is "missing".
Third, order matters because the router uses the first match. The tree lists routes in configuration order, so the panel is a quick way to confirm the wildcard ** sits last.
Matching the tree against the URL
The highlighted nodes show what the router matched. Use this to debug three typical problems.
| Symptom | What to check in the tree |
|---|---|
| A URL shows the "Not found" page | No node matches. Turn on full paths and compare the segments with the URL. Look for a typo, a missing leading segment, or a lazy section that is not mounted where you expect. |
| The wrong component renders | Two nodes could match and the earlier one wins. Check the order, and check any empty-path routes, which match everything beneath them. |
| A route is highlighted but nothing visible changes | The route matched but its component may sit in a `<router-outlet>` that is missing or in a different outlet. Switch to the Components tab and look for the outlet. |
The highlight tells you what the router decided. It does not tell you why a guard rejected a navigation. A rejected navigation can leave the previous route highlighted, or send the user to a redirect target. For the reason, you need navigation events.
Seeing why a navigation went the way it did
The Router tree shows state after the fact. To watch decisions as they happen, enable router tracing during development:
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideRouter, withDebugTracing } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes, withDebugTracing())],
};withDebugTracing() logs every router event to the console, including NavigationStart, RoutesRecognized, GuardsCheckEnd, and NavigationEnd. Navigate to /admin while signed out and you will see the guard check finish, followed by a new navigation to /login. Compare that sequence with the highlighted node in the tree.
Combining the tree with other DevTools panels
The Router tree works best as one step in a sequence.
- Open the Router tree and confirm which route matched the current URL.
- Switch to the Components tab and find the routed component. It sits as a sibling after the
<router-outlet>element, not inside it. Check its inputs and state. - If the route has guards or resolvers, use the injector inspection from the previous chapter to see what the component receives.
- Use the Profiler if navigating to this route feels slow. A heavy routed component shows up as a long change detection cycle.
Step 2 trips people up. In the Components tab, the routed component appears next to the outlet in the DOM, because that is where the router inserts it. If you look inside <router-outlet> you will find nothing.
You can also read the active route from the console. Select a routed component's element in the Elements panel, then run:
// $0 is the selected element. Use a routed component's host element.
const cmp = ng.getComponent($0);
console.log(cmp);This returns the component instance, which is useful for confirming that the route rendered the class you expect. The ng global does not expose the router configuration itself, so use the Router tree panel for that.
Common errors
- Missing children under a lazy route. The chunk has not loaded. Navigate to the route, then reopen the panel.
- A redirect that loops or never fires. An empty-path redirect needs
pathMatch: 'full'. Without it, the empty path matches as a prefix of every URL and redirects everything. - The wildcard route is not last. Routes after
**can never match. The tree makes this ordering visible. - Reading a production build. Angular DevTools needs development mode. A production build shows no tree.
- Confusing a route with a rendered component. Redirects and component-less parent routes appear in the tree but not in the Components tab.
- Assuming the highlight explains a guard failure. It does not. Use
withDebugTracing()or log inside the guard.
Exercises
Exercise
You open the Router tree on the home page of the sample app. The admin node is there, but it has no children. Is the configuration broken, and what do you do to see reports?
Show answer
The configuration is not broken. admin uses loadChildren, so its child routes are not known until the lazy chunk loads. Navigate to /admin (while signed in, or the guard will redirect you to /login), then reopen or refresh the Router tree. The reports node appears under admin.
Exercise
A user visits /admin/reports while signed out and sees the Login page. The Router tree highlights login. Which tool tells you why the router sent them there, and what do you expect to see?
Show answer
Enable withDebugTracing() and read the console. You should see a navigation to /admin/reports where the guards check ends with the guard returning a UrlTree for /login. A second navigation to /login then starts and completes. The Router tree shows only the final state, so it cannot show this sequence.