CodyBugstein
CodyBugstein

Reputation: 23362

Using another module in your module: imports vs. using loadChildren in routes

In my exploration of Angular, I've found two possible ways to use one module inside of another.

(Using the angular-express-starter project for reference)

What are the practical differences between these two methods?

Upvotes: 4

Views: 2322

Answers (2)

Max Koretskyi
Max Koretskyi

Reputation: 105547

What are the practical differences between these two methods?

The biggest difference is that modules loaded via loadChildren will have their own injector whereas providers from imported modules are merged into one root injector. It means that you can't inject providers from the lazy loaded module into other lazy loaded modules.

Other differences:

  • you can't use loadChildren if you don't use routing
  • modules loaded via loadChildren will be loaded only when the corresponding route is navigated to

For more information read

Upvotes: 1

P.S.
P.S.

Reputation: 16392

When you're using loadChildren, it's called "Lazy Loading" and in total it helps us to decrease the startup time. Modules that are lazily loaded will only be loaded when the user navigates to their routes.

For more info about "Lazy Loading": https://angular-2-training-book.rangle.io/handout/modules/lazy-loading-module.html

Upvotes: 0

Related Questions