动态组件创建错误-应该提供ngModule

时间:2019-04-03 13:59:50

标签: angular

我试图在Angular 7中创建动态组件,但出现错误

  

应提供ngModule

export class AppComponent
{ 
    constructor
      (
      private componentFactoryResolver: ComponentFactoryResolver,
      private compiler: Compiler,
      private injector: Injector,
      private sanitizer: DomSanitizer
    ) {

      this.LoadJITComponent();
    }

  public LoadJITComponent() {


    let template = '<h1>This is my component</h1>'
    let tmpCmp: any;

    tmpCmp = Component({ template })(class {

    });

    let dynaModule = NgModule({
      declarations: [tmpCmp],
      schemas: [NO_ERRORS_SCHEMA]

    })(class { });

    let factories = this.compiler.compileModuleAndAllComponentsSync(dynaModule);
    const f = factories.componentFactories[0];
    let cmpRef = f.create(this.injector);

  }
}

当尝试在f.create(this.injector)行上注入组件时,出现以下错误错误:应提供ngModule     在ComponentFactory_.push ../ node_modules/@angular/core/fesm5/core.js.ComponentFactory_.create

0 个答案:

没有答案