Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dev-sampsonorson/87ea4d6ee60a6c36ad48f3d2a26632f7 to your computer and use it in GitHub Desktop.
Save dev-sampsonorson/87ea4d6ee60a6c36ad48f3d2a26632f7 to your computer and use it in GitHub Desktop.
Dynamic load component with wrapper
// Components in ngIf & ngSwitch loaded when used
// With ngxd lib we can create "lazy wrapper"
import { ChangeDetectionStrategy, EventEmitter } from "@angular/core";
// https://www.npmjs.com/package/@ngxd/core
// https://github.com/IndigoSoft/ngxd/issues/30
// You can use Dynamic component loader service
// - Which respect component module, providers, context
@Component({
selector: 'my-lazy-chart-component',
tempate: `
<ng-container *ngIf="component$ | async as c">
<ng-template
[ngxComponentOutlet]="c.componentFactory.componentType"
[ngxComponentOutletInjector]="c.injector">
</ng-template>
</ng-container>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MyLazyChartComponent {
@Input() data?: CharacterData;
@Input() disableZoom: boolean = true;
@Output() readonly fullscreenTrigger = new EventEmitter();
component$ = import(
'./path-to-actual-chart-component/my-chart.component'
).then(m => m.MyChartComponent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment