promise写法
removeUserById(id) {
this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
})
.then(() => {
const { data: res } = this.$http.delete(`users/${id}`).then(res => {
if (res.data.meta.status !== 200) {
return this.$message.error("删除失败");
}
this.$message.success("删除成功");
this.getUserList();
});
})
.catch(() => {
this.$message.info("已取消删除");
});
}
async、await写法
async removeUserById(id) {
const confirmMessage = await this.$confirm(
"此操作将永久删除该用户, 是否继续?",
"提示",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}
).catch(err => err);
if (confirmMessage !== "confirm") {
return this.$message.error("已取消删除");
}
const { data: res } = await this.$http.delete(`users/${id}`);
if (res.meta.status !== 200) {
return this.$message.error("删除失败");
}
this.$message.success("删除成功");
this.getUserList();
}