Angular 8 | Nested Routing with Multiple RouterOutlet using loadChildren having own Router Modules Example Application

In our previous tutorial, we discussed how to implement a very basic Routing Module in an Angular 8 application. Here we will go a step further to discuss some more concepts related to Routing in Angular 8 application.

What we are going to create?

Create an Angular 8 application which will have Nested Routing up to three levels. Each level of components has its own Router Module. We will also discuss How to use lazy loading concept in Angular 8 Routing to load components on run time when they are called.

1) Three levels of Nested Routing.

We will create a demo HRMS(Human Resource Mangement System) application’s navigation structure. The first level of navigations will have following Tab links

Level 1:

– Dashboard(Default page visible on app load)
– My Profile
– My Attendance
– My leaves –>

Level 2: “My leaves” there are two links:

– Apply Leave
– Check Leave Balance –>

Level 3: “Check Leave Balance” further have two tab links

– Casual
– Earned
– Bad Link

Each above level will have it’s own RouterOutlet directive to show active path’s component content. Read more about RouterOutlet directive in the previous post.

2) Separate RouterModules and Component Modules.

Two child component Leaves and Balance will have their own RoutingModule and Component module making lazy loading of these components possible.

3) Handling of “404 Page not found” at each level.

In all Routing Module files, one parent and nested Routing Module files will have wildcard path matching strategy to handle “404 pages not found” error to protect apps from crashes when any wrong path is entered.

Let’s get started!

Start with new Angular 8 application, create using Angular CLI handy useful commands to generate components and modules from CLI or you can use Stacblitz online IDE to create Angular app.

Here we will go with Angular CLI tool. Make sure you have updated the CLI tool by running following NPM command and you are using Node 10 or later.

Create a new Angular application

Run the following command in CLI to create a new Angular application

Create New Components in CLI

Let’s create some components to create an HRMS demo application. Run following Ng CLI generate commands to create components for first, second then the third level of navigation.

Parent components (Level 1)

Child components of leaves (Level 2)

Child components of level 2 leaves/balance (Level 3)

After creating components we have the following file structure:

So,  we have components ready with one app-routing.module.ts file which was created by CLI at the root with the main navigation Routing path config.

Adding RouterOutlet directive at each level

RouterOutlet directive <router-outlet></router-outlet> is placed in the parent component template where other components are loaded on Routing from one view to next view navigation. As in our application, we have three level hierarchy of parent-child components, we will add RouterOutlet directive at three places.

By default, its added in the app.component.html file which is the main component of the application.

Here we also have links to other component views which will get loaded into router-outlet

The routerLink directive is used in Angular template for navigation linking.

The routerLinkActive directive adds a specified class(here ‘active’) when that link is open in view. This directive is helpful in highlighting the active link.

Now for level 2, open the app/leaves/leaves.component.html file and replace with following HTML template code having routerOutlet and navigation links.

The third routerOutlet will sit in the balance component. Open app/leaves/balance/balance.component.html file then replace following HTML template in it.

 

Modularize the application component

The main concept of Angular application is its ability to remain modular and reusable, which makes each component independent and separate from the rest of the application. This makes reusability and maintenance of components very easy.

Here we will make leaves and balance component lazy-loaded by adding their own RouterModules and component module files. These components will have their own RouterModule configuration to load their child components.

What is lazy-loading in Angular?

The concept of lazy-loading is proved very helpful in optimizing large application which scales to complex levels and has a number of components and level hierarchies in them.

Application without lazy-loading loads all components at once on initialization which makes application slow and inefficient in terms of memory usage.

Using lazy-loading components which are required are bundled and loaded runtime on request. The concept of lazy loading works on demand and supply scenario, keeps application light and optimized.

How to make a component in Angular a Lazy-loaded component?

In Angular application converting a simple traditional component into an optimized lazy-loaded component is done by creating its own module. For example for making leaves component a lazy-loaded one, add leaves.module.ts in it. Let’s discuss it in details further.

Create Modules and RouterModules of Child Components

Let’s create modules for Leaves and it’s child Balance component. With Modules create RouterModule as these have subcomponents.

Run following Ng CLI commands to create Module and RouterModules.

In the above command, m is for modules. The –routing option flag will create RouterModule as well.

After running above command now we have the following directory structure.

Update Leaves and Balance Module Files

Leaves and Balance have their own module so we will add their child components in these modules instead of App main module.

Remove child components and update app.module.ts file with following code.

Update app/leaves/leave.module.ts and app/leaves/balance/balance.module.ts files with following code.

Implement Nested Routing with Lazy-Loading 

Let’s setup Routing configuration in RouterModule files.

You can check the previous post on how to setup Routing in Angular application here.

In application’s main Routing module app-routing.module.ts file replace the following code:

In above code, Routes are configured with and without lazy-loading. For leaves, we used the loadChildren property to define the path of the module which is to be lazy-loaded.

Here you may have noticed the new way of importing the module using import method to return module which can detect if there is an error on the imported module.

Before Angular 8 we used the following style, which takes the module as a string which lacks real-time error detection.

 

Other than these use component property which will simply load components on app load.

Basically in one line we can say leaves component handed its responsibility to its module then it will be used to load it and its child component defined in its module file leaves.module.ts

Let’s check RouterModule of leaves component. In leaves-routing.module.ts file replace following content.

children property is having routes with path of child components. For balance we used loadChildren for lazy-loading its module.

Similarly replace the balance-routing.module.ts file with the following code:

That’s it we are ready to run the application by running this command

Check StackBlitz demo link

Conclusion: Here we created an Angular 8 application using Nested Routing deep to three levels and lazy loaded child components by defining their own modules and Router Modules. We used RouterOutlet directive in child components to show three level component loading in the same view.

Thanks for visiting :) Let me know if you have some suggestions or feedback for this post in the comment section. Looking for any tutorial or demo on FreakyJolly, just drop me a message here

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Aaron Camacho LJohan Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Johan
Guest
Johan

Awesome article, really helped me out. However I my app i decided not go with lazy loading since its rather small.

I also noticed that I was unable to make the root default path (path: ”) redirect to one of the child modules. To make this work I had to change the path from ” to ‘my-child-module-main-component’. In your case the parent path in the leaves module with be ‘leaves’ in instead of ”.

Aaron Camacho L
Member
Aaron Camacho L

Hi, how to hide parent, when im showing childs ?