liveBook · Manning

Angular routing

As explained in Essential TypeScript 5, Third Edition

Angular routing is the framework feature that selects and displays components according to URLs and navigates between application views.

Angular routing selects which components appear for the browser’s current URL and lets an application move between views without manually switching components. A route maps a path such as /products or /order to a component; the default path can redirect to /products. section 19.2 section 19.2.2

A small Angular application can organize its views around these routes:

  • /products displays ProductList. section 19.2
  • /order displays OrderDetails. section 19.2
  • /summary/:id displays an order summary using the identifier assigned by the server, such as /summary/5. section 19.2
  • The root URL redirects to /products, making the product list the default view. section 19.2.2

Place <router-outlet></router-outlet> in the application shell so Angular has a location for the component selected by the route. Use routerLink on template elements for direct links, or inject Router when navigation follows component logic. After storing an order, OrderDetails calls navigateByUrl to open its summary URL; a Back button can use routerLink="/products". section 19.2 section 19.2.2

Submitting an order and navigating to its summary

submit() { this.dataSource.storeOrder().subscribe(id => this.router.navigateByUrl(`/summary/${id}`)); } section 19.2

Listing 19.6 Configuring the application in the app.module.ts file in the src/app folder
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FormsModule } from "@angular/forms";
import { DataModelModule } from "./data/data.module";
import { ProductItem } from './productItem.component';
import { CategegoryList } from "./categoryList.component";
import { Header } from "./header.component";
import { ProductList } from "./productList.component";
import { RouterModule } from "@angular/router"
import { OrderDetails } from "./orderDetails.component";
import { Summary } from "./summary.component";

const routes = RouterModule.forRoot([
    { path: "products", component: ProductList },
    { path: "order", component: OrderDetails}, 
    { path: "summary/:id", component: Summary},
    { path: "", redirectTo: "/products", pathMatch: "full"}
]);

@NgModule({
    declarations: [AppComponent,  ProductItem, CategegoryList, 
                   Header, ProductList, OrderDetails, Summary],
    imports: [BrowserModule, AppRoutingModule, FormsModule, 
              DataModelModule, routes],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

Inspect the Angular module configuration that imports routing, registers ProductList, OrderDetails, and Summary, and defines the application’s route mappings. section 19.2.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains route configuration, outlet placement, link navigation, programmatic navigation, redirects, and a missing-outlet failure case.
Examples
Uses /products, /order, /summary/5, an order submission callback, a Back button, and the router-outlet application shell.
What it assumes
Assumes familiarity with Angular components, templates, TypeScript classes, and the application’s DataSource used during order submission.
What you can do after
Apply it to build an Angular application whose URLs select components and whose links or component logic perform navigation.
How it connects
Connects components, URLs, RouterModule, Router, routerLink, router-outlet, redirects, and server-assigned order identifiers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.