Sub topic 12
Inspecting the router tree
This chapter assumes you have Angular DevTools installed and can open the Components tab. It also assumes you know how to declare routes with provideRouter. The router tree is a read-only view of your route configuration, so you can check it without reading source files.
What the Router Tree tab shows
Angular DevTools adds a Router Tree tab next to Components and Profiler when the inspected app uses the Angular Router. The tab draws the route configuration as a tree. Each node is a route. Child routes hang below their parent. The route that matches the current URL is highlighted.
Use it to answer questions such as:
- Which route did the current URL match?
- Is a child route nested where I expect?
- Is this route lazy-loaded?
- Does a redirect or wildcard sit above the route I wanted?
Selecting a node shows its details. The exact fields depend on your DevTools and Angular versions. Expect the path, the component, and whether the route is lazy.
An example configuration
The following routes give the tree several shapes to look at: a redirect, a nested child, a lazy section, and a wildcard.
import { ApplicationConfig } from '@angular/core';
import { Routes, provideRouter, withDebugTracing } from '@angular/router';
export const routes: Routes = [
{ path: '', pathMatch: 'full', redirectTo: 'orders' },
{
path: 'orders',
loadComponent: () =>
import('./orders/orders.component').then((m) => m.OrdersComponent),
children: [
{
path: ':id',
loadComponent: () =>
import('./orders/order-detail.component').then((m) => m.OrderDetailComponent),
},
],
},
{
path: 'admin',
loadChildren: () => import('./admin/admin.routes').then((m) => m.ADMIN_ROUTES),
},
{
path: '**',
loadComponent: () =>
import('./not-found.component').then((m) => m.NotFoundComponent),
},
];
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes, withDebugTracing())],
};withDebugTracing() logs every navigation event to the console. It is a development aid, so remove it before you ship.
Reading the tree
The tree for this configuration looks roughly like the diagram below. Your panel draws it in its own style.
Open the tab, then navigate in the app. Watch the highlight move. Visit /orders/42 and both orders and :id should be highlighted, because the child renders inside the parent.
Compare the diagram to the panel and check these points:
- The wildcard is last. The router matches in order, so a
**route placed early swallows everything below it. - The redirect uses
pathMatch: 'full'. Without it, the empty path matches as a prefix of every URL. - Lazy branches such as
adminmay show little until you visit them. Their children are only known after the code loads.
Common errors
| Symptom | Likely cause | What to check in the tree |
|---|---|---|
| Not-found page shows for a valid URL | Wildcard route declared before the real route | Order of siblings |
| Child component never renders | Parent template lacks `<router-outlet />` | Child is nested correctly, then check the parent template in the Components tab |
| Everything redirects to one page | Redirect missing `pathMatch: 'full'` | The redirect node at the top |
| Lazy section is empty | Route not yet visited, or the load failed | Navigate there, then check the Network panel |
| Router Tree tab is missing | App does not use the Router, or is not in development mode | Requirements from the installation chapter |
Pairing with the Components tab
After a navigation, switch to the Components tab. The routed component appears under a router-outlet. Select it to inspect its inputs. Route parameters bound with withComponentInputBinding() show up there as ordinary inputs, which confirms the parameter reached the component.
Exercise
A user reports that /admin/users shows the not-found page. The Router Tree shows ** as the first child of the root, above admin. What is the problem and how do you fix it?
Show answer
The router matches routes in order, so the ** wildcard matches /admin/users before the admin route is reached. Move the wildcard to the end of the route array so it only catches URLs that nothing else matched. Then reload and confirm that the admin node highlights when you visit the URL.