Sub topic 2
Default Routes, Redirects, and Not-Found Pages
This chapter assumes you already know standalone components, inject(), and basic RxJS. It also assumes you have a Routes array registered with provideRouter.
Redirects and Wildcards
The router checks routes in order and uses the first one that matches. Put a redirect for the empty path near the top, and put the wildcard ** last.
import { Routes } from '@angular/router';
import { HomeComponent } from './home.component';
import { ProductDetailComponent } from './product-detail.component';
import { NotFoundComponent } from './not-found.component';
export const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'products/:id', component: ProductDetailComponent },
{ path: '**', component: NotFoundComponent }
];Route Parameters, Query Parameters, and Fragments
When you register the router with withComponentInputBinding(), it binds path parameters, query parameters, and route data to component inputs that have the same names. You can also read them from ActivatedRoute. The fragment is always read from ActivatedRoute.
import { Component, Input, inject } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-product-detail',
standalone: true,
template: `<h2>Product {{ id }} (tab: {{ tab ?? 'info' }})</h2>
<button (click)="back()">Back</button>`
})
export class ProductDetailComponent {
@Input() id!: string; // from /products/:id
@Input() tab?: string; // from ?tab=reviews
private route = inject(ActivatedRoute);
private router = inject(Router);
section = this.route.snapshot.fragment; // from #specs
back(): void {
this.router.navigate(['/home'], { queryParams: { from: this.id } });
}
}snapshot gives you values once. If the same component instance can be reused for a different :id, use the paramMap observable or input binding instead, because both update when the URL changes.
Navigating from Code
router.navigate(['/products', 42], { queryParams: { tab: 'reviews' }, fragment: 'specs' })builds the URL from an array of segments.router.navigateByUrl('/products/42?tab=reviews')takes a complete URL string.
Both methods return a Promise<boolean> that resolves to false if the navigation was blocked.
Child Routes
A child route renders inside a <router-outlet /> in its parent component's template. This lets a shared layout stay on screen while its inner content changes.
{
path: 'account',
component: AccountLayoutComponent, // template contains <router-outlet />
children: [
{ path: '', redirectTo: 'profile', pathMatch: 'full' },
{ path: 'profile', component: ProfileComponent },
{ path: 'orders', component: OrdersComponent }
]
}Exercise
A user visits /products/7?tab=reviews#specs. Which component values receive "7", "reviews", and "specs"?
Show answer
With withComponentInputBinding, the input id receives "7" and the input tab receives "reviews". The fragment "specs" comes from ActivatedRoute, for example route.snapshot.fragment.
Exercise
Why does putting { path: '**' } first in the Routes array break the app?
Show answer
The router uses the first route that matches, and the wildcard matches every URL. Every navigation would render the not-found component.