问题
How do I reset the values in a class in Typescript and ensure all the values are cleared? Is there a typescript function to do this?
export class Product{
productId: number;
productName: string;
productDescription: string;
}
We have many classes with 50+ fields, looking for efficient way to conduct this.
回答1:
Try like this:
Working Demo
productDetails: Product = {
productId: 1,
productName: "Apple",
productDescription: "Fruit"
};
reset() {
this.productDetails = new Product();
}
回答2:
YOu can create a function and set your all states to default value
export class Product{
productId: number;
productName: string;
productDescription: string;
resetValues() {
this.productId = 0; //default of number datatype is 0
this.productName = null;
this.productDescription = null;
}
}
来源:https://stackoverflow.com/questions/58947764/how-to-reset-clear-the-values-in-a-typescript-class