如何在React Native中将数组指定为对象的属性



这是我的课程模型。时隙是一个时间间隔。不同的课程有不同的时段。我将在预约模型中使用这些插槽,这样用户就可以选择一个插槽并进行预约。我在存储时隙时遇到问题。

class Course {
constructor(
id,
mentorId,
categoryId,
duration,
title,
imageUrl,
description,
slots
) {
this.id = id;
this.mentorId = mentorId;
this.categoryId = categoryId;
this.duration = duration;
this.title = title;
this.imageUrl = imageUrl;
this.description = description;
this.slots = slots;
}

我需要这样的东西:

class Course {
constructor(
id,
mentorId,
categoryId,
duration,
title,
imageUrl,
description,
**slots[]**
) {
this.id = id;
this.mentorId = mentorId;
this.categoryId = categoryId;
this.duration = duration;
this.title = title;
this.imageUrl = imageUrl;
this.description = description;
this.slots = slots;
}

如何将此数组指定为对象属性?或者如何存储此槽属性?谢谢你的帮助。

您可以使用类或接口,并放置如下内容:

class Slot {
attribute_1: type_1;
attribute_2: type_2;
....
} 

interface Slot {
attribute_1: type_1;
attribute_2: type_2;
....
} 

然后在课程类中声明您的插槽阵列如下:

class Course {
id: any;
mentorId: any;
categoryId: any;
duration: any;
title: any;
imageUrl: any;
description: any;
slots: Slot[]; // or slots: Array<Slot>;
constructor(
id: any,
mentorId: any,
categoryId: any,
duration: any,
title: any,
imageUrl: any,
description: any,
slots: Slot[] // or slots: Array<Slot>;
) {
this.id = id;
this.mentorId = mentorId;
this.categoryId = categoryId;
this.duration = duration;
this.title = title;
this.imageUrl = imageUrl;
this.description = description;
this.slots = slots;
}
}

我把any作为类型只是为了示例,所以可以随意更改属性的类型。

您也可以通过执行以下操作来处理null:

slots: Array<Slot> | null;

相关内容

  • 没有找到相关文章

最新更新