使用枚举类型访问对象属性

发布于 2025-02-08 14:52:55 字数 2156 浏览 0 评论 0原文

我正在尝试使用枚举访问对象属性(不确定这是正确的方法),并且我会收到此错误

没有超载与此调用匹配。超载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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2025-02-15 14:52:55

enum在运行时是一个数字,您的定义方式。

这些是字符串。它们没有神奇的关联。

export const MODAL_TYPES =  {
    ADD_NEW_USER: {component: AddNewUserModalComponent, title: "Add New User"}
...
}

您需要参考enum value 在定义modal_types

 [Modals.ADD_NEW_USER] : { component....

或查看字符串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.

export const MODAL_TYPES =  {
    ADD_NEW_USER: {component: AddNewUserModalComponent, title: "Add New User"}
...
}

You need to refer to the enum value when defining your MODAL_TYPES

 [Modals.ADD_NEW_USER] : { component....

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文