多次点击提交按钮导致多次请求问题
问题描述:
在表单数据填写完毕后,快速的点及提交按钮,会发现出现多次重复提交的情况,网上有很多解决方法,例如添加按钮的disable属性,通过返回状态码来控制button的状态,以此防止重复点击:
<el-button type="primary" @click="submitForm" disable="disable">确 定</el-button>
但是经过实际测试发现,快速点击任然可以多次提交,没有解决问题。所以不得不思考别的解决方法。
解决问题:
那么该如何解决这个问题?其实也很简单,只需要在点击按钮后弹出confirm确认框再次确认后提交即可:
/** 提交按钮 */
submitForm: function () {
this.$refs["form"].validate((valid) => {
if (valid) {
this.$confirm('是否确认保存?', "提示",
{confirmButtonText: "确定", cancelButtonText: "取消", type: "success",}
).then(() => {
if (this.form.id != undefined) {
updateList(this.form).then((response) => {
if (response.code === 200) {
this.msgSuccess("修改成功");
this.open = false;
this.getList();
}
});
} else {
addList(this.form).then((response) => {
if (response.code === 200) {
this.msgSuccess("新增成功");
this.open = false;
this.getList();
}
});
}
})}
});
},
这样点击提交按钮后不会立即提交表单数据,而是弹出确认框,在确认后再进行提交,完美解决问题,如果有不足之处,还请各位大佬指出。