javascriptangularoptimizationlazy-loadingangular-lazyloading

How to lazy load Angular Components of the home page?


I have my code as given below in app.component.html i.e. as soon as the user lands on the home screen; I want to lazy load the app-image-gallery & app-users-list and want to load them with some Shimmer effect or Loader as soon as the user reaches the viewport of that particular component. Almost every grown up site use this but this thing is kind of difficult to find for Angular.

I have read many articles on lazy loading of component on button click but haven't found this thing implemented anywhere.

app.component.html

<app-navbar></<app-navbar>
<app-image-gallery></app-image-gallery> //list of images from backend
<app-users-list></app-users-list>   //users list from backend
<app-faq></app-faq>

UPDATE(2022) - After a lot of reasearch, I found these awesome packages for Angular lazy loading of components because packages like @herodevs/hero-loader & ngx loadable are deprecated for newer versions of angular. I will also attach the link to their articles-

@angular-extensions/elements

@juristr/ngx-lazy-el

ngx-element

Articles-

@juristr/ngx-lazy-el

@angular-extensions/elements

These packages are suitable for Angular 9+ versions. I hope it helps someone. And suppose if you want to load components on scroll, On Scroll Load, this is the thing you are looking for and each solution fully tested.


Solution

  • This solution is been tested with angular 9 and above with ivy.

    If you are using older version of angular checkout this article: https://pretagteam.com/question/load-new-modules-dynamically-in-runtime-with-angular-cli-angular-5

    Angular 9+ solution:

    Before you start you should make sure that your lazy loading components are in a separated module which is not imported in your app module.

    First you need to pick a container element and mark it with a template variable to render your lazy component inside it. Something like:

    <div #container></div>
    

    And then in your component class you need to query this container using @ViewChild as ViewContainerRef:

    @ViewChild('container', {read: ViewContainerRef}) container: ViewContainerRef;
    

    Now you are ready to lazy load your component using the webpack dynamic import. which as by default available in an angular app.

    Then you will need to resolve the component factory using the angular ComponentFactoryResolver after you inject it in your component constructor.

    And at the end you will just render the component factory in the view reference you have prepared this.container:

    lazyLoadAppImageGallery() {
        import('path/to/your/app-image-gallery-component')
          .then(({AppImageGallery}) => {
              const componentFactory = 
              this.componentFactoryResolver.resolveComponentFactory(AppImageGallery);
              const { instance } = this.container.createComponent(componentFactory);
           });
    }
    

    After you have rendered your component you might want to pass some data to it. You could use the instance for this:

    instance.propertyName = someValue
    

    Checkout this nice article for more infos: https://medium.com/@ckyidr9/lazy-load-feature-modules-without-routing-in-angular-9-ivy-220851cc7751

    Update

    Here is a working solution with dynamic render on scroll

    https://stackblitz.com/edit/angular-ivy-megwrz

    If you might use lazy loading with routes, just checkout angular simple documentation

    https://angular.io/guide/lazy-loading-ngmodules

    You could also combine both solutions. In case you want to dynamically load a component in a lazy loaded module loaded by route.

    Update since Angular v13

    Since v13 ComponentFactoryResolver is deprecated. Create component does require resolving component factory. You can use the component class directly thanks to the Ivy engine. Which means instead of writing:

    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(AppImageGallery);
          const { instance } = this.container.createComponent(componentFactory);
    

    you could just write:

    this.viewContainerRef.createComponent(AppImageGallery);