npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-ruh-lazy-component

v1.0.1

Published

A simple utility to lazy load components into angular without routing dependency.

Downloads

7

Readme

NgRuhLazyComponent

A simple utility to dynamically lazy load components into an angular app without routing dependency.

  • Much inspiration drawn from https://netbasal.com/the-need-for-speed-lazy-load-non-routable-modules-in-angular-30c8f1c33093 and https://medium.com/@esanjiv/complete-beginner-guide-to-publish-an-angular-library-to-npm-d42343801660

Demo Screen

Demo

Benefits

  • Reduce your inital app load time by loading only the essential components first
  • Reduce the initial payload size
  • Leverage lazy loading features WITHOUT routing dependency (Not all apps are route based )
  • Ideal for apps that need to build dynamic UIs defined via JSON configs or based on user driven highly customizable UIs
  • Prevents unnessasary memory hogging by loading only what is required

Usage

On app.module file import the module

import { NgRuhLazyComponentModule } from 'ng-ruh-lazy-component'; //<-- Add this line

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    NgRuhLazyComponentModule  //<-- Add this line
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
  • On anywhere in your component templates add the following
<ng-ruh-lazy-component 
    componentPath="src/app/modules/lazy-module-1/lazy-module-1.module#LazyModule1Module#Comp1Component"
    [data] = "dataToPushToLoadedComponent">
    <div error>There was a problem loading the component</div>
    <div loading>Loading Component, Please stand by</div>
</ng-ruh-lazy-component>
  • componentPath : should take the format of <Path to .module file>#<Name of the module>#<Component Key>
  • data : data To Push To Loaded Component
  • error directive (Optional) : Allows you to specify html to show when an error is encountered
  • loading directive (Optional) : Allows you to specify html to show when the module is loading

Prerequisites

The smallest unit of Angular code that could be lazy loaded is an Angular Module (NGModule) and NOT a Component, Hence ALWAYS make sure you create a module that encapsulates one or more of the Components you need to reload.

  • Create your module, use ng g m modules/lazy-module1
  • Create 1 or more components under the module, use ng g c modules/lazy-module1/comp1, ng g c modules/lazy-module1/comp2
  • on the modules/lazy-module1/lazy-module1.module
@NgModule({
  declarations: [Comp1Component, Comp2Component],
  entryComponents: [Comp1Component, Comp2Component], // <-- Add this line
  imports: [
    CommonModule
  ]
})
export class LazyModule1Module { 
  static componentMap = {                 // <-- Add static property componentMap 
    "Comp1Component" : Comp1Component,    //    to map the string component key with 
    "Comp2Component" : Comp2Component     //     the actual component ref
    }                                     // you can also add a function named getComponentEntry(compType:string) 
}                                               
  • On your angular.json on the project root, add the module path the the lazyModules array in the project.<your-app-name>.architect.build.options.lazyModules
 project:{
     <your-app-name>:{
         architect:{
             build:{
                 options:{
                     lazyModules:[
                         ...
                         '<path to .module file(s)>',
                         'src/app/modules/lazy-module1/lazy-module1.module',
                         ...
                     ]
                 }
             }
         }
     }
 }
  • Follow above steps for each module that need to be lazy loaded