Angular Routing
Routing turns one Angular app into many pages. You will define routes, link between pages, and read values from the path and the query string.
Updated
Basic Routing in Angular
- Providing the Router (Apprentice): An Angular app is a single page that shows different components for different addresses. The router reads the URL and shows the matching page component in <router-outlet>, without the browser loading a new page. ng new sets it all up for you.
- Defining the Routes (Apprentice): A route pairs a path with the component to show for it. List them in app.routes.ts, the home page at the empty path and a page-not-found route with ** last, because the router uses the first route that matches. A route can also set the page's title or redirect an old address.
- Linking to Other Pages (Apprentice): A plain href makes the browser reload the whole app. routerLink lets the router change the page instead, routerLinkActive marks the link of the page you're on, and Router's navigate goes to another page from code, such as after saving.
Route Parameters in Angular
- Route Parameters in Angular: an overview (Journeyman): An address can carry a value: /categories/3 means category 3. One route with a parameter, categories/:categoryId, serves every category, like {id} in an ASP.NET Core route. It takes three steps: the route, the component that reads the value, and the links that put it in.
- Reading the Route Parameter (Journeyman): With withComponentInputBinding, the router hands a route's parameters to the page component as inputs, so categoryId is an input like any other, turned into a number with numberAttribute. Load the data in an effect, because Angular reuses the page when only the id changes.
- Defining the Links (Journeyman): Build a link with an id in it by giving routerLink a list of parts: [routerLink]="['/categories', category.categoryId]" makes /categories/3. The same list works with Router's navigate, and the router joins and escapes the parts for you.
Query Parameters in Angular
- Query Parameters in Angular: an overview (Journeyman): Values that are optional, such as a filter, a search or a page number, go in the query string: /products?categoryId=2&page=3. They need no route of their own, and the page shows with or without them. A route parameter says which item; a query parameter says how to show the page.
- Reading Query Parameters (Journeyman): Input binding hands query parameters to the page as inputs too, or undefined when they're missing, so give each a default with ?? in a computed. Link with routerLink and [queryParams], and change them from code with navigate and queryParamsHandling 'merge'.