Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5: Lazy-loading of component without routing

In my web application I have a "Terms and Conditions" popup that opens by link click in the footer (so it's a core component). Popup comprises of several tabs, each of them is a pretty big template file.

I wonder if it's possible to move tab templates to separate chunk and organize their lazy-loading? I'm not sure if default Angular lazy-loading is acceptable for me because I don't want to have separate route for the popup.

like image 383
user3429127 Avatar asked Jun 01 '18 17:06

user3429127


People also ask

How do you use a lazy load module without routing?

define where we want to load our component in the template with the ng-template tag, define its view query through ViewChild decorator, which gives us access to the DOM and defines the container to which the component will be added, finally, dynamic import the component and add it to the container.

Can you lazy load a component in Angular?

To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. content_copy const routes: Routes = [ { path: 'items', loadChildren: () => import('./items/items. module').

Can components be lazy loaded?

Lazy loading is the technique used in optimizing your web and mobile apps, this works by rendering only needed or critical user interface items first, then quietly rendering the non-critical items later. As we build code components the application grows, and the bundle gets very cumbersome in size.

Is lazy loading necessary Angular?

Lazy loading is an important Angular feature that helps to reduce the initial load time since it loads only the necessary files first. Other required modules are loaded on demand when you navigate to their particular route. Now, you can take advantage of this feature to improve your app's load time.


1 Answers

You can wait for the user to click on the link and as soon as Click Event occurs, load the required component in the view. Things to keep in mind:

  1. You need to have a placeholder defined for the component in the view.
  2. The terms and conditions component needs to be defined as Entry level component for it's Module or the module where it is used.

     entryComponents: [
        ChildComponent
      ],
    
  3. Make sure to refer to the placeholder in your component and attach the terms and condition component dynamically.

      <div>
          <ng-template #viewContainerRef></ng-template>
       </div>
    

and

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

and create the component dynamically:

createComponent() {

    let componentFactory = this.CFR.resolveComponentFactory(ChildComponent);
    let componentRef: ComponentRef<ChildComponent> = this.VCR.createComponent(componentFactory);
    let currentComponent = componentRef.instance;

    currentComponent.selfRef = currentComponent;
  // to track the added component, you can reuse this index to remove it later
    currentComponent.index = ++this.index; 

    // providing parent Component reference to get access to parent class methods
    currentComponent.compInteraction = this;

}

there is an example for you here: https://add-or-remove-dynamic-component-parent-child.stackblitz.io

like image 114
nircraft Avatar answered Oct 11 '22 18:10

nircraft