Add content for lesson 11. Update previous lessons and code examples be in line with the latest lesson formatting. PR Close #49980
5.8 KiB
Lesson 10 - Add routes to the application
This tutorial lesson demonstrates how to add routes to your app.
Time required: expect to spend about 25 minutes to complete this lesson.
Before you start
This lesson starts with the code from the previous lesson, so you can:
- Use the code that you created in Lesson 9 in your integrated development environment (IDE).
- Start with the code example from the previous lesson. Choose the from Lesson 9 where you can:
- Use the live example in StackBlitz, where the StackBlitz interface is your IDE.
- Use the download example and open it in your IDE.
If you haven't reviewed the introduction, visit the Introduction to Angular tutorial to make sure you have everything you need to complete this lesson.
If you have any trouble during this lesson, you can review the completed code for this lesson, in the for this lesson.
After you finish
At the end of this lesson your application will have support for routing.
Conceptual preview of routing
This tutorial introduces routing in Angular. Routing is the ability to navigate from one component in the application to another. In single page applications (SPA), only parts of the page is updated to represent the requested view for the user.
The Angular Router enables users to declare routes and specify which component should be displayed on the screen if that route is requested by the application.
In this lesson, you will enable routing in your application to navigate to the details page.
Lesson steps
Perform these steps on the app code in your IDE.
Step 1 - Create a default details component
-
From the terminal, enter the following command to create the
DetailsComponent:ng generate component details --standalone --inline-style --inline-template
This component will represent the details page that provides more information on a given housing location.
Step 2 - Add routing to the application
-
In the
src/appdirectory, create a file calledroutes.ts. This file is where we will define the routes in the application. -
In
main.ts, make the following updates to enable routing in the application:-
Import the routes file and the
provideRouterfunction: -
Update the call to
bootstrapApplicationto include the routing configuration:
-
-
In
src/app/app.component.ts, update the component to use routing:-
Add a file level import for
RoutingModule: -
Add
RouterModuleto the@Componentmetadata imports -
In the
templateproperty, replace the<app-home></app-home>tag with the<router-outlet>directive and add a link back to the home page. Your code should match this code:
-
Step 3 - Add route to new component
In the previous step you removed the reference to the <app-home> component in the template. In this step, you will add a new route to that component.
-
In
routes.ts, perform the following updates to create a route.-
Add a file level imports for the
HomeComponent,DetailsComponentand theRoutestype that you'll use in the route definitions. -
Define a variable called
routeConfigof typeRoutesand define two routes for the app:The entries in the
routeConfigarray represent the routes in the application. The first entry navigates to theHomeComponentwhenever the url matches''. The second entry uses some special formatting that will be revisted in a future lesson.
-
-
Save all changes and confirm that the application works in the browser. The application should still display the list of housing locations.
Lesson review
In this lesson, you enabled routing in your app as well as defined new routes. Now your app can support navigation between views. In the next lesson, you will learn to navigate to the "details" page for a given housing location.
You are making great progress with your app, well done.
If you are having any trouble with this lesson, you can review the completed code for it in the .
Next steps
More information
For more information about the topics covered in this lesson, visit: