laravel - 使用 SweetAlert、Vue 和 Laravel 进行确认

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

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

最佳答案

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

关于laravel - 使用 SweetAlert、Vue 和 Laravel 进行确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52158109/

相关文章:

php - 如何使用 `created_at` 列获取最新数据

firebase - Vuex,在 Action 中改变状态

javascript - Vue.js 中的国际化使用 vue-i18n 从 API 服务器获取 JSON 对象

javascript - 带有 AJAX 内容的 Sweet Alert 2

javascript - 如何在 PHP 中使用 sweetalert2

php - 日期时间突变器 Laravel

php - Laravel 5 动态运行迁移

laravel - 如何在查询生成器 laravel 上获取每个组的多个数据

javascript - 使用正则表达式和 Vue 处理字符串切片和格式化

javascript - 响应甜蜜警报 2 后触发功能