In our previous tutorial, we discussed how to implement a very basic Routing Module in an Angular 10/9/8/7/6/5/4 application. Here we will go a step further to discuss some more concepts related to Routing in Angular 8 application.
Multiple outlets, but only one URL?
The key thing to realize about top-level auxiliary routes is that effectively each one has its own URL to match to, starting at
/. Auxiliary routes can also be configured not at the top-level, but lets focus on that scenario in this post.
Imagine that you divide your browser window into multiple mini-browser windows, each with its own separate URL. Then you provide separate routing configuration for each of those windows because you would want those windows to be navigated separately. Here are some examples.
Router Navigation with the routerLink directive
As we have included
RouterModule in our app, we can use the
routerLink directive to define router navigation links in our template. There are a couple of ways of doing this:
We can either hardcode a string directly in the template, like its the case of the home route or the courses route. But we can also pass it an expression. If so we need to pass it an array containing the multiple URL path parts that we want to navigate to: in this case we want to navigate to the
Another way of doing router navigation is to use the router programmatic API to do so. For that we just have to inject the router into our component, and make use of either the
navigateByUrl navigation methods:
One of the things that we usually want to do when navigating between two routes is to pass navigation parameters to the target route.
Angular Router Configuration: an Introduction
The Angular Router is what makes an Angular Application a Single Page Application, or SPA. For learning all about the benefits of the SPA architecture, have a look at this post.
The first thing that we should do is simply write some routing configuration. What we are doing in this initial configuration is to map certain URL paths to Angular components: meaning that if there is a path match then the component gets displayed:
This configuration means:
Homecomponent gets displayed
AllLessonscomponent gets displayed
But where do these components get displayed?
Once the router has a URL match, it will try to display the corresponding matching components. for that it will look in the template for a
Router outlet is a dynamic component that the router uses to display in this case either the
Home or the
AllLessons components. There is nothing special about these components, these could be any component.
To finish the setup of the router, we also need to add its directives and injectables into the Angular bootstrap system. We do so by importing the
RouterModule into the application root module:
Notice that we configure the module by using the
forRoot function instead of simply adding the
RouterModule. To learn more about why this is necessary, have a look at this other post on @NgModule.
With this, if we access the
/lessons URLs, we would get the corresponding component displayed.
But here is where for new users of the router things might start to go wrong.
Can Angular have multiple router outlet?
Can we use multiple router outlet?
How do I use multiple router outlets in Angular 9?
How many router outlets can be used in an Angular application?