angulardynamiccomponents

Angular create component with ViewContainerRef when the component class name in variable


I have a question please.

Is there a component name array:

private componentArray: string[] = ['PageOneComponent', 'PageTwoComponent', 'PageThreeComponent', 'PageFourComponent'];

How can I pass the component names to create the component with ViewContainerRef.createComponent?

Thank you in advance


Solution

  • It needs to be classes. You could convert your array to:

    private componentArray: Type<any>[] = [PageOneComponent, PageTwoComponent, PageThreeComponent, PageFourComponent];
    

    Or if you need labels :

    private componentArray: Type<any>[] = [{label: 'foo', component: PageOneComponent}, ... ];