← Back to Angular, page 1

angular-0012 · Easy fresher

How do you set up routing in a standalone Angular app, including route parameters and a wildcard route?

Define a Routes array, register it with provideRouter in the application config, place a router-outlet where pages should appear, and navigate with routerLink. Route parameters are written as :id, and with withComponentInputBinding they arrive directly as component inputs. A "**" route catches unknown URLs.

Full answer

Routing lets one page show different components depending on the URL, without a full page reload.

Set-up has four pieces:

  1. A Routes array maps paths to components: { path: 'courses/:id', component: CourseDetailComponent }.
  2. provideRouter(routes) in the ApplicationConfig providers turns routing on.
  3. <router-outlet /> in a template marks where the matched component is rendered. Import RouterOutlet in that component.
  4. routerLink="/courses" on links navigates without reloading. routerLinkActive="active" adds a CSS class on the current link. Import RouterLink and RouterLinkActive.

Route parameters. In courses/:id, the part after the slash is a parameter. There are two ways to read it: the older ActivatedRoute (route.paramMap observable or route.snapshot.paramMap), or the newer way: add withComponentInputBinding() to provideRouter, and the parameter is bound to an input called id. Query parameters and resolver data can be bound in the same way.

Order matters. The router picks the first match, so the wildcard { path: '**', component: NotFoundComponent } must be last. A redirect from the empty path needs pathMatch: 'full', or it matches every URL.

To navigate from code, inject Router and call router.navigate(['/courses', id]). Extra points that come up in interviews:

  • Query parameters and the fragment are separate from path parameters: /courses?level=easy#reviews. Navigate with [queryParams] on routerLink, or router.navigate(['/courses'], { queryParams: { level: 'easy' } }). They stay optional and are not part of the route match.
  • routerLink accepts an array: [routerLink]="['/courses', course.id]" builds /courses/42 and encodes special characters safely.
  • Relative links such as routerLink="../" are resolved against the current route, which is useful in nested routes.
  • Lazy load a page with loadComponent: () => import('./course-detail').then(m => m.CourseDetailComponent). The code for that page is downloaded only when the user visits it.
  • A title on a route sets the document title, and redirectTo with pathMatch: 'full' sends '' to a default page.
  • Guards (canActivate) protect routes, and resolvers load data before the page shows. These are separate topics, but a good answer names them.
  • For a link to show as active, use routerLinkActive and, for the home link, [routerLinkActiveOptions]="{ exact: true }", otherwise / is active on every page.

If a parameter changes while the same component stays on screen (from /courses/1 to /courses/2), Angular reuses the component. With input binding the input updates, so use a computed or effect on it and not ngOnInit.

typescript
// file: app.routes.ts
import { Routes } from '@angular/router';
import { CourseDetailComponent } from './course-detail.component';
import { CourseListComponent } from './course-list.component';
import { NotFoundComponent } from './not-found.component';

export const routes: Routes = [
  { path: '', redirectTo: 'courses', pathMatch: 'full' },
  { path: 'courses', component: CourseListComponent, title: 'Courses' },
  { path: 'courses/:id', component: CourseDetailComponent },
  { path: '**', component: NotFoundComponent },
];

// file: app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [provideRouter(routes, withComponentInputBinding())],
};

// file: app.component.ts
import { Component } from '@angular/core';
import { RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router';

@Component({
  selector: 'app-root',
  imports: [RouterOutlet, RouterLink, RouterLinkActive],
  template: `
    <nav>
      <a routerLink="/courses" routerLinkActive="active">Courses</a>
    </nav>
    <router-outlet />
  `,
})
export class AppComponent {}

// file: course-list.component.ts
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';

@Component({
  selector: 'app-course-list',
  imports: [RouterLink],
  template: `<a [routerLink]="['/courses', 42]">Angular basics</a>`,
})
export class CourseListComponent {}

// file: course-detail.component.ts
import { Component, input } from '@angular/core';

@Component({
  selector: 'app-course-detail',
  template: `<h1>Course {{ id() }}</h1>`,
})
export class CourseDetailComponent {
  // Filled from the :id route parameter by withComponentInputBinding().
  id = input.required<string>();
}

// file: not-found.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-not-found',
  template: `<p>Page not found</p>`,
})
export class NotFoundComponent {}

Angular 20.3. In `main.ts` pass `appConfig` to `bootstrapApplication`. The `id` input is filled from the URL `/courses/42`.

Key points

  • `provideRouter(routes)` plus `<router-outlet />` plus `routerLink` are the core pieces.
  • `:id` defines a parameter. `withComponentInputBinding()` delivers it as an input.
  • First match wins, so put `**` last and use `pathMatch: 'full'` on empty-path redirects.
  • Navigate in code with `Router.navigate`.

Common mistakes

  • Using `href` on links. That reloads the whole page and loses app state.
  • Putting the `**` route first, which makes every route show the not-found page.
  • Forgetting `pathMatch: 'full'` on a `path: ''` redirect.

Follow-ups

  • What is the difference between `paramMap` and `snapshot.paramMap`?
  • How do query parameters differ from route parameters?
  • How do you make a route lazy loaded?

Tips

  • Mention `withComponentInputBinding`. It shows you know the current way to read params.
  • Explain the "first match wins" rule using the wildcard route.
  • Say that `href` would reload the page, which is the whole reason for `routerLink`.

Evaluation rubric

  • Names provideRouter, router-outlet, and routerLink.
  • Reads route parameters in a current way.
  • Understands route order and pathMatch.