与SweetAlert,Vue和Laravel确认



我正在尝试在确认后删除我的数据,为此我想使用sweetalert。

1

如果我使用简单的警报,例如:

deletePage(index) {
if (confirm("Do you really want to delete it?")) {
let page = this.pages[index];
axios.delete(`/api/pagedelete/${page.id}`).then(response => {
this.pages.splice(index, 1);
});
}
},

工作正常

阿拉伯数字

当我想使用甜蜜警报时,例如:

deletePage(index) {
let page = this.pages[index];
swal({ 
title: "Are you sure ?",
text: "You will not be able to recover this page !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it !",
cancelButtonText: "No, cancel !",
closeOnConfirm: false,
closeOnCancel: false 
}, 
function(isConfirm) {
if (isConfirm) {
axios.delete(`/api/pagedelete/${page.id}`).then(response => {
this.pages.splice(index, 1);
});
}
else {
swal("Cancelled", "Your page has not been deleted !", "error");   
}
}
);
}

不行!

我得到的错误是:

uncaught exception: SweetAlert: Unexpected 2nd argument (function (isConfirm) {
var _this2 = this;
if (isConfirm) {
axios.delete('/api/pagedelete/' + page.id).then(function (response) {
_this2.pages.splice(index, 1);
});
} else {
swal("Cancelled", "Your page has not been deleted !", "error");
}
})

知道如何解决这个问题吗?

文档显示它使用承诺进行确认:

swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
swal("Poof! Your imaginary file has been deleted!", {
icon: "success",
});
} else {
swal("Your imaginary file is safe!");
}
});

然后,您的示例将是:

deletePage(index) {
let page = this.pages[index];
swal({ 
title: "Are you sure ?",
text: "You will not be able to recover this page !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it !",
cancelButtonText: "No, cancel !",
closeOnConfirm: false,
closeOnCancel: false 
}).then((confirmed) => {
if (confirmed) {
axios.delete(`/api/pagedelete/${page.id}`).then(response => {
this.pages.splice(index, 1);
});
} else {
swal("Cancelled", "Your page has not been deleted !", "error");   
}
});
}

最新更新