使用枚举类型访问对象属性
我正在尝试使用枚举访问对象属性(不确定这是正确的方法),并且我会收到此错误
没有超载与此调用匹配。超载1 of 2,'(componentType: 类型,选项?:{index?:号码|不明确的;注射器?: 喷油器|不明确的; ngmoduleref?:ngmoduleref |不明确的; projectablenodes?:node [] [] |不明确的; } |不明确的): componentRef< ...>',给出以下错误。 类型'{modalType:模态的参数;组件:AddNewuserModalComponent类型;标题:字符串; }'不能分配给 类型“类型”的参数。
基本上,当我打开模态时,我只想发送一个参数,例如模态。add_new_user,并基于此属性名称从modaltypes访问对象,以便我可以拥有组件和标题。
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"},
}
我希望能够使用枚举类型访问对象的模态容器,
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]);
}
}
也尝试像这样创建我的对象,
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"
}
]
但我仍然不知道如何根据枚举访问每个对象
我 使用模式枚举的对象属性?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
enum
在运行时是一个数字,您的定义方式。这些是字符串。它们没有神奇的关联。
您需要参考
enum
value 在定义modal_types
时或查看字符串
enums
enum x {y =“ mystring”}
,但是您仍然想进行上述更改,而不是依赖字符串的隐式相关性。您总是可以记录事物以查看发生的事情,您会发现您以模态类型的传递是数字,而您查找的对象在钥匙中的对象将没有匹配的键。An
enum
at run time is a number, how you've defined it.These are strings. They are not magically correlated.
You need to refer to the
enum
value when defining yourMODAL_TYPES
Or look into string
enums
enum X { Y="MyString"}
, but you'd still want to make the above change and not rely on the implicit correlation of strings. You can always log things to see what's going on, you'd see that your passed in modal types are numbers, and the object you're looking up the keys in would have no matching key.