帮我优化这段代码 handleWorkOrderMessage() { // 待办理、待审核二级弹窗 console.log(this.pWorkOrder.orderState); if (this.pWorkOrder.orderState === '20') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,下单!'; } // 接单 if (this.pWorkOrder.orderState === '30') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,接单!'; } // 反馈 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 = '按要求完成,反馈!'; } // 审核关闭 if (this.pWorkOrder.orderState === '50') { this.popupSitch.description.show = true; this.popupSitch.description.label = '经办意见'; this.description = '按要求完成,审核关闭!'; } this.$refs.orderPopup.open(); },
时间: 2024-01-09 10:04:12 浏览: 64
可以尝试以下优化:
1. 将重复的代码提取出来,避免重复写相同的语句。例如,多个条件下都需要设置 `popupSitch.description.show` 为 `true`,可以在代码开头先设置为 `false`,然后根据条件进行修改。
2. 使用 switch 语句替代多个 if 语句,可以让代码更加清晰易懂。
3. 如果有多处使用的字符串,可以将其提取为常量或者变量,方便修改和管理。
下面是优化后的代码示例:
```
handleWorkOrderMessage() {
const DES_LABEL = '经办意见';
const DESC_COMPLETE = '按要求完成,';
const DESC_ORDER = '下单!';
const DESC_ACCEPT = '接单!';
const DESC_FEEDBACK = '反馈!';
const DESC_CLOSE = '审核关闭!';
this.popupSitch = {
description: {
show: false,
label: ''
},
image: {
show: false,
label: ''
}
};
switch (this.pWorkOrder.orderState) {
case '20':
this.popupSitch.description.show = true;
this.popupSitch.description.label = DES_LABEL;
this.description = DESC_COMPLETE + DESC_ORDER;
break;
case '30':
this.popupSitch.description.show = true;
this.popupSitch.description.label = DES_LABEL;
this.description = DESC_COMPLETE + DESC_ACCEPT;
break;
case '40':
this.popupSitch.description.show = true;
this.popupSitch.description.label = DES_LABEL;
this.popupSitch.image.show = true;
this.popupSitch.image.label = '反馈图片';
this.description = DESC_COMPLETE + DESC_FEEDBACK;
break;
case '50':
this.popupSitch.description.show = true;
this.popupSitch.description.label = DES_LABEL;
this.description = DESC_COMPLETE + DESC_CLOSE;
break;
default:
break;
}
this.$refs.orderPopup.open();
},
```
阅读全文