优化这段代码 if (this.form.salesTime == "sales") { this.dateChange(this.form.saleStartDate, this.form.saleEndDate); } else { this.dateChange(this.form.playStartDate, this.form.playEndDate); } } /** * 开始时间和结束时间才能查询同一天的数据 */ dateChange(saleStartDate?:string,saleEndDate?:string,playStartDate?:string,playEndDate?:string) { 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") ); } } }
时间: 2024-03-01 12:51:38 浏览: 101
可以将if-else语句替换为三元表达式,简化代码逻辑,如下:
```typescript
const startDate = this.form.salesTime == "sales" ? this.form.saleStartDate : this.form.playStartDate;
const endDate = this.form.salesTime == "sales" ? this.form.saleEndDate : this.form.playEndDate;
this.dateChange(startDate, endDate);
```
同时,可以将参数移除,直接在函数内部获取开始时间和结束时间,如下:
```typescript
dateChange() {
const startDate = new Date(this.form.salesTime == "sales" ? this.form.saleStartDate : this.form.playStartDate).getTime();
const endDate = new Date(this.form.salesTime == "sales" ? this.form.saleEndDate : this.form.playEndDate).getTime();
if (this.form.productType == 3 && this.selectUsers.length > 0) {
if (startDate === endDate) {
this.getSessionManageList(date.format(new Date(startDate), "YYYY-MM-DD"));
}
}
}
```
最后,可以将函数名改为更具体的名称,比如`querySameDayData`,以便更好地表达函数的作用。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)