Angular Lazy Loading Boilerplate [Updated November]

Developers always look for a boilerplate which can reduce their initial project setup time and efforts. Being a developer, I always search for basic boilerplates in Angular. I found the Angular boilerplate very helpful. It has a command to generate lazy loading boilerplate with routing module.

Angular is a JavaScript framework and it is very lightweight in performance and has many great features for programmers. Angular is continuously developing its new versions.

Let’s build a lazy loading boilerplate with routing module in angular:

Generate Angular boilerplate

Its time to generate the boilerplate. Run below command-

The project with routing module is created and you will see the output like this-

lazy loading boilerplate in angular 6

Generate child modules with routing

Now generate child pages with routing modules. Please notice there is --routing parameter is appended in the command. That parameter is used to create routing of each child module that will be is used for lazy loading. If you don’t append the --routing parameter in command, it will not generate routing of each child module and hence, it will not be lazy loading.

Run these commands one by one to generate modules-

The output of above commands will look like this-

lazy loading boilerplate in angular 6

Generate components for each child

In above step, we have generated child modules with routing. Now we need to generate components for each module. Run below commands one by one to generate components-

The output will end up like this-

lazy loading boilerplate in angular 6

We have now generated modules with routing and their components.

Setup child routing

Now its time to setup the child routing in child modules.

Open child routing files and add the following lines of code.

home-routing.module.ts

login-routing.module.ts

signup-routing.module.ts

Now we have done setup of child routing.

Setup routing in main module

We need to setup the routing in parent routing module ie. app-routing.module.ts file

Open app-routing.module.ts file and paste below code in that-

Parent routing is now setup.

Remove html from app.component.html file

Open app.component.html file in editor and remove all the HTML and paste the following code in that-

Its time to run the project

Run the project using command ng server --open and wait for it to open page on browser. The project will start running on browser with routing functionality.

Test the routing using below links-

Home page- http://localhost:4200

Login page- http://localhost:4200/login

Signup page- http://localhost:4200/signup

lazy loading boilerplate in angular 6

You will see the page with routing on browser-

lazy loading boilerplate in angular 6

Download the complete source code from here- https://github.com/enginesoftsolutions/angular6-lazy-loading-boilerplate

Also Read:

Conclusion

Get Angular Lazy Loading Boilerplate to start implementing new features.

Leave a Reply

Your email address will not be published. Required fields are marked *