使用 SweetAlert、Vue 和 Laravel 进行确认
Confirmation with 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);
});
}
},
工作正常
2
当我想使用 sweetalert 时:
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");
}
})
知道如何解决这个问题吗?
The documentation shows it using promises for confirmation:
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");
}
});
}
我正在尝试在确认后删除我的数据,为此我想使用 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);
});
}
},
工作正常
2
当我想使用 sweetalert 时:
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");
}
})
知道如何解决这个问题吗?
The documentation shows it using promises for confirmation:
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");
}
});
}