我想将一个对象传递给我的数据模型(而不是传递单独的参数)。但是,通过这样做,我必须编写三倍的代码。是否有一种不那么冗长的方式来编写对象析构化代码(如参数化方式)
首先 - 参数化方式(供参考):
export class DataModel {
constructor(
public suppliers: SupplierModel[],
public inStock: boolean,
public myFavorites: boolean,
) {}
}
对象 - 去构造方式:
export class DataModel {
public suppliers: SupplierModel[];
public inStock: boolean;
public myFavorites: boolean;
constructor(
{suppliers, inStock, myFavorites}:
{suppliers: SupplierModel[], inStock: boolean, myFavorites: boolean
) {
this.suppliers = suppliers;
this.inStock = inStock;
this.myFavorites = myFavorites;
}
}