Angular 6 ES6 初始化对象数组
Angular 6 ES6 initiate object arrays
我真的是 javascript 的新手。
我有一个嵌套的 class 结构,我需要在其中使用 json 对象启动。我的问题是如何启动 EventDate 对象数组并在 CustomerEvents 构造函数
中分配给 this.dates
export default class CustomerEvent {
constructor(customer_event: any) {
this.event_title = customer_event.event_title;
this.customer = customer_event.customer;
this.total_budget = customer_event.total_budget;
this.no_of_people = customer_event.no_of_people;
this.dates = /**array of new EventDate(customer_event.dates) **/;
}
event_title: string;
customer: Customer;
total_budget: number;
no_of_people: number;
dates: EventDate[];
}
class EventDate {
constructor(date: any) {
this.start_date = date.start_date;
this.end_date = date.end_date;
}
start_date: Date;
end_date: Date;
}
如果有人可以帮助我,那将非常有帮助。谢谢
只需分配新的空数组,如下所示:
constructor(customer_event: any) {
this.event_title = customer_event.event_title;
this.customer = customer_event.customer;
this.total_budget = customer_event.total_budget;
this.no_of_people = customer_event.no_of_people;
this.dates = [];
}
如果需要强制转换传入数组,可以这样做:
...
this.dates = customer_event.dates.map(date => new EventDate(date));
...
Angular Style Guide recommends 使用 interface
s 作为数据模型而不是 class
es:
Consider using an interface for data models.
也就是说,您可以像这样重构您的代码:
export interface EventDate {
start_date: Date;
end_date: Date;
}
export interface CustomerEvent {
event_title: string;
customer: Customer;
total_budget: number;
no_of_people: number;
dates: EventDate[];
}
现在说到初始化,你可以这样做:
const customerEvent: CustomerEvent = {
event_title: 'Some Title',
customer: { /*An Object representing a Customer Type*/ }
total_budget: 123,
no_of_people: 456,
dates: [{
start_date: new Date(),
end_date: new Date()
}]
};
自己创建这些实例:
constructor(customer_event: any) {
this.event_title = customer_event.event_title;
this.customer = customer_event.customer;
this.total_budget = customer_event.total_budget;
this.no_of_people = customer_event.no_of_people;
this.dates = customer_event.dates.map(date => new EventDate(date));
}
我真的是 javascript 的新手。
我有一个嵌套的 class 结构,我需要在其中使用 json 对象启动。我的问题是如何启动 EventDate 对象数组并在 CustomerEvents 构造函数
中分配给 this.dates export default class CustomerEvent {
constructor(customer_event: any) {
this.event_title = customer_event.event_title;
this.customer = customer_event.customer;
this.total_budget = customer_event.total_budget;
this.no_of_people = customer_event.no_of_people;
this.dates = /**array of new EventDate(customer_event.dates) **/;
}
event_title: string;
customer: Customer;
total_budget: number;
no_of_people: number;
dates: EventDate[];
}
class EventDate {
constructor(date: any) {
this.start_date = date.start_date;
this.end_date = date.end_date;
}
start_date: Date;
end_date: Date;
}
如果有人可以帮助我,那将非常有帮助。谢谢
只需分配新的空数组,如下所示:
constructor(customer_event: any) {
this.event_title = customer_event.event_title;
this.customer = customer_event.customer;
this.total_budget = customer_event.total_budget;
this.no_of_people = customer_event.no_of_people;
this.dates = [];
}
如果需要强制转换传入数组,可以这样做:
...
this.dates = customer_event.dates.map(date => new EventDate(date));
...
Angular Style Guide recommends 使用 interface
s 作为数据模型而不是 class
es:
Consider using an interface for data models.
也就是说,您可以像这样重构您的代码:
export interface EventDate {
start_date: Date;
end_date: Date;
}
export interface CustomerEvent {
event_title: string;
customer: Customer;
total_budget: number;
no_of_people: number;
dates: EventDate[];
}
现在说到初始化,你可以这样做:
const customerEvent: CustomerEvent = {
event_title: 'Some Title',
customer: { /*An Object representing a Customer Type*/ }
total_budget: 123,
no_of_people: 456,
dates: [{
start_date: new Date(),
end_date: new Date()
}]
};
自己创建这些实例:
constructor(customer_event: any) {
this.event_title = customer_event.event_title;
this.customer = customer_event.customer;
this.total_budget = customer_event.total_budget;
this.no_of_people = customer_event.no_of_people;
this.dates = customer_event.dates.map(date => new EventDate(date));
}