To allow customer customization, I need a way to load component dynamically using a string, taken from backend.
I see this usefull guide: https://medium.com/angular-in-depth/loading-components-dynamically-in-angular-cd13b9fdb715
but, as other tutorial like the official one, they always assume you already know classnames.
Look at this code from the tutorial:
async loadComponent(vcr: ViewContainerRef, isLoggedIn: boolean) {
const { GuestCardComponent } = await import('./guest-card/guest-card.component');
const { UserCardComponent } = await import('./user-card/user-card.component');
...
The string inside import is not a problem but the definition of class is.
I need a placeholder in my template for loading a component I know I have in the FileSystem but I don't know its name.
Something like this:
From backend i get:
var dynamic_loading_component = { "classname" : "AdidasCardComponent" , "path" : "/components/adidas.component" }
then I would like to use like this:
const { dynamic_loading_component["classname"] } = await import(dynamic_loading_component["path"]);
vcr.clear();
let component : any = dynamic_loading_component["classname"];
return vcr.createComponent(this.cfr.resolveComponentFactory(component))
Some sort of Reflection.
Is it possible?