Angular Multiple Router Outlets

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 /lessons path.

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 navigate or 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:

  • if you navigate to /home then the Home component gets displayed
  • if you navigate to /lessons then AllLessons component gets displayed
  • and if you navigate elsewhere you are going to get an error
  • 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 component:

    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 /home or /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.

    FAQ

    Can Angular have multiple router outlet?

    Yes you can as said by @tomer above. i want to add some point to @tomer answer. firstly you need to provide name to the router-outlet where you want to load the second routing view in your view.

    Can we use multiple router outlet?

    Yes! We can use multiple router-outlets in same template by configuring our routers and simply add the router-outlet name. You can see in the example.

    How do I use multiple router outlets in Angular 9?

    There is no limit in angular but there are some best practices of using multiple place one in app.

    How many router outlets can be used in an Angular application?

    There is no limit in angular but there are some best practices of using multiple place one in app.

    Related Posts