Dynamic Routing with API - Product Details Page
A product listing page and a product detail page are created.
When a product is clicked, it opens a new page with full details.
What is Being Built
A product list page shows product names as links.
On clicking any product, a detail page opens.
The detail page shows image, title, price, category, description, rating, and stock.
This is similar to e-commerce websites.
Components Created
Two components are created:
- Product list component
- Product detail component
Command used:
Routing Setup
Routes are defined in app.routes.ts.
Empty path shows product listing.
details/:id is used for dynamic routing.
Router Outlet Setup
RouterOutlet is imported in app.ts.
HTML uses:
Moving Product List Code
API call and product list logic are moved from app.ts
They are placed inside product-list.component.ts.
Signals and service are imported properly.
Showing Product List as Links
Only product title is shown as a link.
RouterLink is imported in the component.
Dynamic ID in Route
Route is changed to dynamic using :id.
Product ID is passed in the URL.
Getting ID in Product Details Component
ActivatedRoute is used to read route parameter.
ID is received correctly when clicking different products.
Fetching Product Details Using ID
Same API is used to get all products.
Selected product is filtered using ID.
Filtered product is the clicked one.
Storing Selected Product in Signal
Only one product is stored.
Displaying Product Details in HTML
Optional chaining is used because data can be undefined.