我有一个数组名称 dish 和一个表格。表单提交后,数据推送到盘中。我尝试使用 push 方法将其添加到数组中,但它有错误。我怎么能用 typescript 做到这一点? 非常感谢你。 类对象。
export interface Dish {
id: number;
name: string;
image: string;
category: string;
label: string;
price: string;
featured: boolean;
description: string;
comments: Comment[];
}
我已经从类评论中创建了一个对象名称 commentData,以便在提交后接收来自表单的所有数据。我还从类 Dish 创建了一个对象名称 dish。如何将对象 commentData 推送到对象 dish.comments
export interface Comment {
rating: number;
comment: string;
author: string;
date: string;
}
最佳答案
let myArray = [];
let commentData = {} as Dish;
commentData.id = 3;
commentData.name = 'something';
myArray.push(commentData);
它会工作...
关于typescript - 如何使用 typescript 将对象插入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46249472/