After the docs UI redesign `h2` tags got a border top. `border-top: 1px solid #dbdbdb;`; In the sections of Getting Started guide in order to separate `What's next` from above content an `<hr />` tag was used, that now becomes unnecessary. This commit removes unnecessary `<hr />` tags. PR Close #40693
6.3 KiB
Adding navigation
This guide builds on the first step of the Getting Started tutorial, Get started with a basic Angular app.
At this stage of development, the online store application has a basic product catalog.
In the following sections, you'll add the following features to the application:
- Type a URL in the address bar to navigate to a corresponding product page.
- Click links on the page to navigate within your single-page application.
- Click the browser's back and forward buttons to navigate the browser history intuitively.
{@a define-routes}
Associate a URL path with a component
The application already uses the Angular Router to navigate to the ProductListComponent.
This section shows you how to define a route to show individual product details.
-
Generate a new component for product details. In the file list, right-click the
appfolder, chooseAngular GeneratorandComponent. Name the componentproduct-details. -
In
app.module.ts, add a route for product details, with apathofproducts/:productIdandProductDetailsComponentfor thecomponent. -
Open
product-list.component.html. -
Modify the product name anchor to include a
routerLinkwith theproduct.idas a parameter.The
RouterLinkdirective helps you customize the anchor element. In this case, the route, or URL, contains one fixed segment,/products. The final segment is variable, inserting theidproperty of the current product. For example, the URL for a product with anidof 1 would be similar tohttps://getting-started-myfork.stackblitz.io/products/1. -
Verify that the router works as intended by clicking the product name. The application should display the
ProductDetailsComponent, which currently says "product-details works!"Notice that the URL in the preview window changes. The final segment is
products/#where#is the number of the route you clicked.
View product details
The ProductDetailsComponent handles the display of each product.
The Angular Router displays components based on the browser's URL and your defined routes.
In this section, you'll use the Angular Router to combine the products data and route information to display the specific details for each product.
-
In
product-details.component.ts, importActivatedRoutefrom@angular/router, and theproductsarray from../products. -
Define the
productproperty. -
Inject
ActivatedRouteinto theconstructor()by addingprivate route: ActivatedRouteas an argument within the constructor's parentheses.ActivatedRouteis specific to each component that the Angular Router loads.ActivatedRoutecontains information about the route and the route's parameters.By injecting
ActivatedRoute, you are configuring the component to use a service. The Managing Data step covers services in more detail. -
In the
ngOnInit()method, extract theproductIdfrom the route parameters and find the corresponding product in theproductsarray.The route parameters correspond to the path variables you define in the route. To access the route parameters, we use
route.snapshot, which is theActivatedRouteSnapshotthat contains information about the active route at that particular moment in time. The URL that matches the route provides theproductId. Angular uses theproductIdto display the details for each unique product. -
Update the
ProductDetailsComponenttemplate to display product details with an*ngIf. If a product exists, the<div>renders with a name, price, and description.The line,
<h4>{{ product.price | currency }}</h4>, uses thecurrencypipe to transformproduct.pricefrom a number to a currency string. A pipe is a way you can transform data in your HTML template. For more information about Angular pipes, see Pipes.
When users click on a name in the product list, the router navigates them to the distinct URL for the product, shows the ProductDetailsComponent, and displays the product details.
For more information about the Angular Router, see Routing & Navigation.
What's next
You have configured your application so you can view product details, each with a distinct URL.
To continue exploring Angular:
- Continue to Managing Data to add a shopping cart feature, manage cart data, and retrieve external data for shipping prices.
- Skip ahead to Deployment to deploy your application to Firebase or move to local development.

