我试图在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