如何解决动态Angular组件中的导入模块和调用方法
我创建了一个Angular 7动态组件,该组件使用Angular材质MatDialog来调用另一个组件。这给了我模块'class_2'导入的意外值'MatDialog'。请添加一个@NgModule批注。但是,如果我不添加MatDialog,则会得到:无法解析class_1的所有参数:(?)。
请帮助。谢谢。
动态组件创建:
@ViewChild('vc',{read: ViewContainerRef}) vc: ViewContainerRef;
constructor(private _compiler: Compiler,private _injector: Injector,private _m: NgModuleRef<any>) {
}
ngAfterViewInit() {
const template = '<span (click)="openDialog()">Click Here</span>';
const tmpCmp = Component({template: template})(class {
constructor(private _dialog: MatDialog) {}
openDialog() {
this._dialog.open(AnotherComponent,{});
}
});
const tmpModule = NgModule({
imports: [CommonModule,MatDialog],declarations: [tmpCmp],})(class {
});
this._compiler.compileModuleAndAllComponentsAsync(tmpModule)
.then((factories) => {
const f = factories.componentFactories[0];
const cmpRef = this.vc.createComponent(f);
cmpRef.instance.name = 'dynamic';
})
}
解决方法
您是否应该在MatDialogModule
NgModule
数组中导入imports
?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。