i am using the Angular Compiler to compile components in runtime. This Code works fine, but if I want to use AOT-Prerendering the Component wont work, because Angular does not load the Compiler in AOT-Build.
I've read about some Workarounds that wont Work in Angular5+ anymore. Do you have any solutions for this problem?
Best Regards
export class RuntimeCompilerComponent { template: string = ""; @ViewChild('dynamicComponent', { read: ViewContainerRef }) container: ViewContainerRef; constructor(private compiler: Compiler) { } //Ruft die addComponent Methode auf createComponent() { this.addComponent(this.template, null); } // Komponente wird dynamisch erzeugt und geladen // Sollten sich die properties ändern muss ggf. die Changedetection manuell aufgerufen werden. private addComponent(template: string, properties: any = {}) { @Component({ template }) class TemplateComponent { } @NgModule({ imports: [ AppModule, CommonModule, ReactiveFormsModule, FormsModule, BrowserModule, ], declarations: [TemplateComponent] }) class TemplateModule { } const mod = this.compiler.compileModuleAndAllComponentsSync(TemplateModule); const factory = mod.componentFactories.find((comp) => comp.componentType === TemplateComponent ); const component = this.container.createComponent(factory); Object.assign(component.instance, properties); } }