Created
June 17, 2023 07:30
-
-
Save dev-sampsonorson/87ea4d6ee60a6c36ad48f3d2a26632f7 to your computer and use it in GitHub Desktop.
Dynamic load component with wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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