I am trying to access an object property using an enum (not sure if this is the right approach) and I get this error
No overload matches this call. Overload 1 of 2, '(componentType: Type, options?: { index?: number | undefined; injector?: Injector | undefined; ngModuleRef?: NgModuleRef | undefined; projectableNodes?: Node[][] | undefined; } | undefined): ComponentRef<...>', gave the following error. Argument of type '{ modalType: Modals; component: typeof AddNewUserModalComponent; title: string; }' is not assignable to parameter of type 'Type'.
Basically, when I open a modal, I want to send just a parameter e.g. Modals.ADD_NEW_USER and based on that to access the object from ModalTypes with this property name so I can have the component and title.
export enum Modals {
ADD_NEW_USER,
DELETE_CLASS,
DELETE_STUDENT,
BLOCK_STUDENT,
ADD_NEW_CLASS,
CLASS_PROFILE,
EDIT_CLASSROOMS
}
export const MODAL_TYPES = {
ADD_NEW_USER: {component: AddNewUserModalComponent, title: "Add New User"},
DELETE_CLASS: {component: DeleteClassModalComponent, title: "Delete Class"},
DELETE_STUDENT: {component: DeleteStudentModalComponent, title: "Delete Student"},
BLOCK_STUDENT: {component: BlockStudentModalComponent, title: "Block Student"},
ADD_NEW_CLASS: {component: AddNewClassModalComponent, title: "Add new class"},
CLASS_PROFILE: {component: ClassProfileModalComponent, title: "Class Profile"},
EDIT_CLASSROOMS: {component: EditClassroomModalComponent, title: "Edit Classrooms"},
}
My modal container that in which I want to be able to access the object with the enum type
export class ModalContainerComponent implements OnInit {
@ViewChild('modalContent', {
read: ViewContainerRef
})
modalContent!: ViewContainerRef;
modalTypes = MODAL_TYPES;
constructor(
@Inject(MAT_DIALOG_DATA) public modalType: any,
private viewContainerRef: ViewContainerRef) {
}
ngOnInit(): void {
console.log(this.modalType)
// const modal = ModalTypes[this.modalType];
this.viewContainerRef.createComponent(this.modalTypes[this.modalType]);
}
}
I also tried to create my object like this
export const MODAL_TYPES = [
{
modalType: Modals.ADD_NEW_USER,
component: AddNewUserModalComponent,
title: "Add New User"
},
{
modalType: Modals.ADD_NEW_USER,
component: AddNewUserModalComponent,
title: "Add New User"
}
]
but i still don't know how to access each object based on the enum
How can I access Modal types object property using Modals enum?