首页 > 解决方案 > 找不到类{constructor(){this.iterablePlainMap=e.message}ngOnInit(){}} 的组件工厂。你把它添加到@NgModule.entryComponents 了吗?

问题描述

它可以正常工作,ng serve但之后会出现此错误ng build ---prod

这是组件类,我在运行时 component.ts上创建动态组件

   dynamicComponent;
   dynamicModule: NgModuleFactory<any>;

   downloadFile() {
      this.http.get(this.fileUrl, { responseType: 'text' }).subscribe(res => {
      this.dynamicComponent = this.createNewComponent(res);
      this.dynamicModule=this.compiler.compileModuleSync(this.createComponentModule(this.dynamicComponent));
    });
   }

创建新模块功能

 protected createComponentModule(componentType: any) {
    @NgModule({
      imports: [
        FormsModule,
        CommonModule
      ],
      declarations: [
        componentType,
      ],
      entryComponents: [componentType]
    })
    class RuntimeComponentModule {
    }
    // a module for just this Type
    return RuntimeComponentModule;
  }

创建新的组件函数

 protected createNewComponent(text: string) {
    const that = this;
    @Component({
      selector: 'dynamic-component',
      template: `${text}`,
      interpolation: ['{[{', '}]}']
    })
    class DynamicComponent implements OnInit {
      ngOnInit() {
      }
    }
    return DynamicComponent;
  }

标签: angulartypescript

解决方案


推荐阅读