handleStart(index, row) { if (row.name === '服务器') { this.$message({ message: '还不能控制服务器开机哦', type: 'warning' }); return false } else if (row.status === '运行中') { this.$message({ message: '服务已经启动了哦', type: 'warning' }); return false } else { axios.get('http://127.0.0.1:8000/api/v1/monitor/controCourse?contorName=' + row.name + "&contorType=start",).then(res => { this.$message({ message: '启动成功', type: 'success' }); }) } },
时间: 2023-06-24 21:07:05 浏览: 55
这段代码是一个Vue.js方法,在点击某个按钮时会被触发。它用于控制某个服务的启动。如果要启动的服务是服务器,则会弹出警告提示;如果服务已经处于运行状态,则也会弹出警告提示。只有当服务未运行时,才会通过axios发送请求启动服务。在请求成功后,会通过$message方法弹出成功提示。其中,请求地址是 http://127.0.0.1:8000/api/v1/monitor/controCourse ,请求参数包括 contorName(服务名称)和 contorType(控制类型,此处为启动)。
相关问题
handleStart(index, row) { if (row.name === '服务器') { this.$message({ message: '还不能控制服务器开机哦', type: 'warning' });
这段代码是一个 Vue.js 中的方法,用于处理用户点击某个按钮时触发的事件。具体来说,当用户点击某个按钮时,会调用该方法,并传入两个参数:index 和 row。其中,index 表示按钮所在的位置,row 表示该按钮对应的数据行。
该方法首先判断当前点击的按钮所在的数据行是否为“服务器”,如果是,则弹出一个提示框,告诉用户暂时还不能控制服务器的开机操作。提示框的内容为“还不能控制服务器开机哦”,类型为警告。该方法的作用是保护服务器,防止用户误操作导致不必要的损失。
优化一下这段代码:async handleConfirm(){ this.productForm.finalAmount = this.finalAmount this.productForm.couponId = this.selectCoupon.couponId const { msg } = await samplePayment(this.productForm); this.closeDialog() if(msg === '201'){ this.$modal.confirmCustom('账户余额不足!', 'Tips', { confirmButtonText: "充值", cancelButtonText: "关闭", type: 'warning' }).then(() => { this.$router.push({ path: "/payment/payment-recharge"}); }).catch(() => {}); return; }else if(msg === '202'){ this.$modal.notifyWarning('代金劵不存在') return; }else if(msg === '203'){ this.$modal.notifyWarning('代金劵异常') return; }else if(msg === '204' || msg === '205'){ this.$modal.notifyWarning('样本异常') return; } this.$modal.msgSuccess("支付成功"); },
async handleConfirm() {
this.productForm.finalAmount = this.finalAmount;
this.productForm.couponId = this.selectCoupon.couponId;
const { msg } = await samplePayment(this.productForm);
this.closeDialog();
switch (msg) {
case '201':
await this.$modal.confirmCustom(
'账户余额不足!',
'Tips',
{
confirmButtonText: "充值",
cancelButtonText: "关闭",
type: 'warning'
}
);
this.$router.push({ path: "/payment/payment-recharge" });
break;
case '202':
this.$modal.notifyWarning('代金劵不存在');
break;
case '203':
this.$modal.notifyWarning('代金劵异常');
break;
case '204':
case '205':
this.$modal.notifyWarning('样本异常');
break;
default:
this.$modal.msgSuccess("支付成功");
break;
}
}
主要优化:
1.使用 switch 替换 if-else,使代码更加简洁易懂。
2.将需要执行的代码从 Promise 的 then 方法中提取出来,避免 Promise 嵌套过深,增加代码可读性。
3.将需要执行的代码放到 default 分支中,避免代码重复。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)