优化这段代码 // 剧场销售开始日期 salesStartChange() { this.timeProcessing(this.form.saleStartDate, this.form.saleEndDate); } // 剧场销售结束日期 salesEndChange(date: string) { this.timeProcessing(this.form.saleStartDate, this.form.saleEndDate); } // 剧场游玩开始日期 playStartChange() { this.timeProcessing(this.form.playStartDate, this.form.playEndDate); } //剧场游玩结束日期 playEndChange() { this.timeProcessing(this.form.playStartDate, this.form.playEndDate); } timeProcessing(startDate: any, endDate: any) { if (this.form.productType == 3 && this.selectUsers.length > 0) { if (new Date(startDate).getTime() === new Date(endDate).getTime()) { this.getSessionManageList( date.format(new Date(startDate), "YYYY-MM-DD") ); } } }
时间: 2024-03-01 16:51:13 浏览: 250
可以将四个函数合并成一个函数,然后在每次日期变化时调用该函数,同时去掉日期转换的重复计算。代码如下:
```
dateChange() {
if (this.form.productType == 3 && this.selectUsers.length > 0) {
const saleStartDate = new Date(this.form.saleStartDate).getTime();
const saleEndDate = new Date(this.form.saleEndDate).getTime();
const playStartDate = new Date(this.form.playStartDate).getTime();
const playEndDate = new Date(this.form.playEndDate).getTime();
if (saleStartDate === saleEndDate) {
this.getSessionManageList(
date.format(new Date(saleStartDate), "YYYY-MM-DD")
);
} else if (playStartDate === playEndDate) {
this.getSessionManageList(
date.format(new Date(playStartDate), "YYYY-MM-DD")
);
}
}
}
```
这样可以避免重复计算,提高代码的可读性和可维护性。
阅读全文