Routing introduction
- βοΈUnderstand routing with AngularLearn how routing works in Angular
- βοΈDefine routesDefine the routes for your application
What is routing?
Routing is the process of determining what content to display based on the URL. Angular is a single-page application (SPA) framework. By changing the URL, the application can display different content without reloading the page. It provides a Router API to change the content based on the URL.
The project currently displays a list of tasks. We want to be able to switch between displaying this list or the form to add a new task.
Router outlet
The Angular RouterModule provides a directive called router-outlet
. This directive is used to display the content of the current route. When the user navigates to a different route, the content of the router-outlet
is replaced with the content of the new route.
This router-outlet
is a placeholder for our content.
π Instructions
-
Update the
src/app/app.component.html
file.
As the router-outlet
is a placeholder, it means weβll keep seeing the header on all pages, but the content of the router-outlet
will change based on the URL.
Define the routes
We need to define the routes for our application. Each route is defined by a path and a component as an object :
The path is the URL that the user will navigate to. For example /add-task
path means that the user will navigate to http://localhost:4200/add-task
.
The component provided is the component that will be displayed in the router-outlet
placeholder when the user navigates to that URL.
The route definition lies in the app-routing.module.ts
file.
π Instructions
-
Update the
src/app/app-routing.module.ts
file.
Add links to navigate between routes
We need to add links to trigger the navigation between the different routes. We could manually change the URL in the browser but users expect to have clickables links to navigate between pages.
The <a>
HTMl tag is used to create links and is associated to the routerLink directive to trigger Angular routing. This directive take the path of the route to navigate to as a value.
π Instructions
-
Update the
src/app/app.component.html
file. -
Click on both links to see the content of the
TaskListComponent
andTaskFormComponent
displayed in therouter-outlet
.
βοΈ What you learned
In this chapter, you learned how to add routing to your application. You learned how to define the routes and how to navigate between them using the routerLink directive. You also learned how to display the content of the current route using the router-outlet
directive.
π¦ Quiz
-
What is the purpose of the
router-outlet
directive?- A. To define the routes of the application
- B. To display the content of the current route
- C. To navigate between routes
- D. To display the content of the previous route
-
What is the purpose of the
routerLink
directive?- A. To define the routes of the application
- B. To display the content of the current route
- C. To navigate between routes
- D. To display the content of the previous route
-
What is the purpose of the
Routes
array in theapp-routing.module.ts
file?- A. To define the routes of the application
- B. To display the content of the current route
- C. To navigate between routes
- D. To display the content of the previous route
-
What is the difference of prefixing a route with
/
or not in therouterLink
directive?- A. There is no difference
- B. The route is relative to the current URL
- C. The route is absolute
- D. The route is relative to the root URL