请帮我优化这段代码 // 下单 if (this.pWorkOrder.orderState === '20') { this.pWorkOrder.checkRequirements = this.description; } // 接单 if (this.pWorkOrder.orderState === '30') { this.pWorkOrder.checkRequirements = this.description; } // 反馈 if (this.pWorkOrder.orderState === '40') { if (this.pictureDet.length <= 0) { this.$tip.error(`${this.popupSitch.image.label}必填`); return; } this.pWorkOrder.checkRequirements = this.description; this.pWorkOrder.mPicture = this.pictureDet.join(','); } // 审核关闭 if (this.pWorkOrder.orderState === '50') { this.pWorkOrder.checkRequirements = this.description; }
时间: 2024-01-09 22:04:12 浏览: 24
可以将重复的代码提取出来,以减少冗余:
```
this.pWorkOrder.checkRequirements = this.description;
```
然后可以使用 switch 语句来替换多个 if 语句:
```
switch(this.pWorkOrder.orderState) {
case '20':
case '30':
case '50':
this.pWorkOrder.checkRequirements = this.description;
break;
case '40':
if (this.pictureDet.length <= 0) {
this.$tip.error(`${this.popupSitch.image.label}必填`);
return;
}
this.pWorkOrder.checkRequirements = this.description;
this.pWorkOrder.mPicture = this.pictureDet.join(',');
break;
default:
break;
}
```
这样代码就更简洁易读了。
相关问题
帮我优化这段代码 handleWorkOrderMessage() { // 待办理、待审核二级弹窗 console.log(this.pWorkOrder.orderState); if (this.pWorkOrder.orderState === '20') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,下单!'; this.orderState = '30'; } // 接单 if (this.pWorkOrder.orderState === '30') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,接单!'; this.orderState = '40'; } // 反馈 if (this.pWorkOrder.orderState === '40') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.popupSitch.image.label = '反馈图片'; this.popupSitch.image.show = true; this.description = '按要求完成,反馈!'; this.orderState = '50'; } // 审核关闭 if (this.pWorkOrder.orderState === '50') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,审核关闭!'; this.orderState = '60'; } this.$refs.orderPopup.open(); },
可以考虑使用 switch case 语句来替换 if else 语句,这样代码更加简洁易读。同时,可以将重复的代码提取成函数,避免代码冗余。下面是重构后的代码:
```
handleWorkOrderMessage() {
const { orderState } = this.pWorkOrder;
switch (orderState) {
case '20':
this.showDescriptionPopup('按要求完成,下单!', '30');
break;
case '30':
this.showDescriptionPopup('按要求完成,接单!', '40');
break;
case '40':
this.showImagePopup('按要求完成,反馈!', '50');
break;
case '50':
this.showDescriptionPopup('按要求完成,审核关闭!', '60');
break;
default:
break;
}
this.$refs.orderPopup.open();
},
showDescriptionPopup(description, orderState) {
this.popupSitch.description.show = true;
this.popupSitch.description.label = '经办意见';
this.description = description;
this.orderState = orderState;
},
showImagePopup(description, orderState) {
this.popupSitch.description.show = true;
this.popupSitch.description.label = '经办意见';
this.popupSitch.image.label = '反馈图片';
this.popupSitch.image.show = true;
this.description = description;
this.orderState = orderState;
},
```
可以看到,重构后的代码更加简洁易读,同时避免了重复的代码。
v-if="scope.row.checkInState==='待入住'"怎么同时判断scope.row.orderState==='已支付'
你可以使用逻辑运算符 &&(与)来同时判断两个条件,将它们放在同一个v-if指令中即可,示例代码如下:
```
v-if="scope.row.checkInState==='待入住' && scope.row.orderState==='已支付'"
```
这样,只有当两个条件都满足时,才会渲染指定的元素。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)