通过SweetAlert,Vue和Lravael进行确认

时间:2018-09-04 02:42:26

标签: laravel vue.js sweetalert

我正在尝试确认后删除我的数据,为此,我想使用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");
 }
 })

有什么解决方法吗?

1 个答案:

答案 0 :(得分:1)

The documentation shows it using promises for confirmation

LoginModule

您的示例将是:

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!");
  }
});