@wishtack/reactive-component-loader
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

Build Status Greenkeeper badge

What is Reactive Component Loader?

@wishtack/reactive-component-loader is an Angular module that allows:

  • declarative dynamic component insertion,
  • component lazy loading and not only with the router (even with AOT enabled),
  • passing @Inputs and @Outputs easily to the dynamically inserted component (using ng-dynamic-component).

Getting Started

1. Install

yarn add @wishtack/reactive-component-loader

or npm install --save @wishtack/reactive-component-loader

2. Setup

Add ReactiveComponentLoaderModule.forRoot() to the root module.

import { ReactiveComponentLoaderModule } from '@wishtack/reactive-component-loader';

@NgModule({
    ...
    imports: [
        ReactiveComponentLoaderModule.forRoot()
    ]
    ...
})
export class AppModule {
}

3. Declare lazy loaded modules

@NgModule({
    ...
    imports: [
        ReactiveComponentLoaderModule.withModule({
            moduleId: 'todo-form-v1',
            loadChildren: './todo-form-v1/todo-form-v1.module#TodoFormV1Module'
        }),
        ReactiveComponentLoaderModule.withModule({
            moduleId: 'todo-form-v2',
            loadChildren: './todo-form-v2/todo-form-v2.module#TodoFormV2Module'
        }),
    ]
    ...
})
export class TodoListModule {
}

4. Lazy load components

Using <wt-lazy>...

@Component({
    template: `
    <wt-lazy
        [location]="todoFormComponentLocation"
        [inputs]="{keywords: keywords}"
        [outputs]="{keywordsChange: onKeywordsChange}">
    `
})
export class SomeFeatureComponent {
    
    todoFormComponentLocation = {
        moduleId: 'todo-form-v1',
        selector: 'wt-todo-form-v1'
    };
    
    onKeywordsChange = (keywords: string) => {
        ...
    }
    
}

... or ngComponentOutlet

@Component({
    template: `
    <ng-container *ngIf="todoFormComponentRecipe$ | async as recipe">
        <ng-container *ngComponentOutlet="recipe.componentType; ngModuleFactory: recipe.ngModuleFactory"
    </ng-container>
    `
})
export class SomeFeatureComponent {
    
    todoFormComponentRecipe$ = this._reactiveComponentLoader.getComponentRecipe({
        moduleId: 'todo-form-v1',
        selector: 'wt-todo-form-v1'
    });
    
    constructor(private _reactiveComponentLoader: ReactiveComponentLoader) {
    }
    
}

Package Sidebar

Install

npm i @wishtack/reactive-component-loader

Weekly Downloads

63

Version

1.1.2

License

MIT

Unpacked Size

810 kB

Total Files

57

Last publish

Collaborators

  • wishtack-admin
  • yjaaidi