1

I'm moving my app to lazy loading and I found 2 ways of having components loaded.

One is having X components and just one global components.module (e.g. https://www.9lessons.info/2017/12/ionic-angular-lazy-loading-child-components.html) that we'd need to import on our pages. But what happens if we just want one component?

That's the other way which I saw on some @mhartington's repo. We could have again one .module per component as we have with pages: https://github.com/mhartington/lazyLoad2-components/blob/master/src/components/music-card/music-card.module.ts

Is this second way better than the first one? What's the point of having all component loaded on one page if we're using lazy loading?

2
  • 1
    Depends what you mean by 'better'... Commented Aug 26, 2018 at 15:42
  • better in terms of speed Commented Aug 26, 2018 at 18:02

1 Answer 1

3

What I'm writing below is a short excerpt from Angular's Style Guide and will help you decide which your Lazy Loading Strategy.

The basic idea behind Lazy Loading modules in Angular is to load a module only if it's required. The ideal way of implementing it is to create different modules. Now, there would generally be these types of modules in your Angular App:

  1. App/Main/Root Module - This would be the orchestrator of your Angular App. All the modules, required to initially load your Angular App would reside in this module.
  2. Shared Module - This module will contain all the components/pipes/directives etc, that would be used by other modules in your Angular App.
  3. Core Module - This module will contain all the single-use components/pipes/directives/services etc. Also, this modules should be imported only once, and that too, inside your App/Main/Root Module.
  4. Utility Modules - Ideally there should be a module for any specific third-party feature that you're using. This helps to keep the App/Main/Root Module clean. So ideally, if you're using Angular Material for eg, then you should create a module for that that imports all the modules for all the components, that you'd be using in your Angular App, and then export it, from this module, so that any module, that wants to use Angular Material Components, could import it and use it.
  5. Feature Modules - These are the modules that should ideally be lazy loaded. An Angular App should ideally be broken down into logically separated features and each of these modules should comprise of one such logically separated feature. This allows us to then lazy load them.

Keeping all these points in mind, you should break your Angular App down in this way so that it's clear which modules are Feature Modules and can be lazy loaded.

Sign up to request clarification or add additional context in comments.

5 Comments

Amazing info, thanks for that. Root Module is the normal one with everything loaded (no lazy loading). Shared Module, as it says "A lazy loaded feature module that imports that shared module will make its own copy of the service and likely have undesirable results", is not the best approach. Core Module sounds quite handy for providers and components related to this. Utility Modules sounds also good for other libraries but in my case I just use Ionic and Angular. If I were using more probably it'd be also helpful. But as you said, the best approach here is Feature Modules
I'd need to create separate .modules for components, pipes and directives as well, not just for pages and include then separately on my page.modules. This info is quite important because you something end up with some random tutorials online (like the first one I shared) with mixed information about lazy loading and modules and probably not using the best approach. Thanks for this again!
Glad I helped. Also, as you mentioned, yes it's not a good practice to add Services into shared modules, that's why I've not mentioned services in SharedModule but I've done so for CoreModule. Hope we're on the same page WRT that.
any idea why is this failing? I'm trying to load a child component using the Feature Modules way: forum.ionicframework.com/t/doubt-about-lazy-loaded-components/…
Can you please ask a separate question here along with the folder structure and some code for ref?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.