如何将组件名称作为参数传递并在函数中接收



我需要将组件名称作为参数传递给函数,然后接收并使用它。但只接收字符串。以下是代码

(click)="this.myapp.openModal('AddContactModelComponent','xl')"

我在这里收到它,用作组件

openModal(component:any,size:string) {
// const modalRef = this.modalService.open(ModalComponent);
const modalRef = this.modalService.open(
component,
{
size: size, 
});
this.modal_title ='test';
}

正在传入一个字符串作为component参数(示例中为'AddContactModelComponent'(。

你很可能需要一个openModal可以像一样使用的映射器

openModal(componentName: string, size: string) {
const mapper = {
'AddContactModelComponent': AddContactModelComponent,
'AnotherString': AnotherComponent,
};
const modalRef = this.modalService.open(
mapper[componentName],
{
size: size,
});
}

然后你可以根据需要在你的模板中调用它

(click)="this.myapp.openModal('AddContactModelComponent','xl')"

最新更新