简体   繁体   English

在 Vue js 中使用 Sweet 警报删除确认

[英]Delete confirmation with Sweet alert in Vue js

I have a comment delete button in vue components.我在 vue 组件中有一个评论删除按钮。

<button class="button" style="background-color: grey;" @click="destroy">Delete</button>

When the button clicked will call the method "destroy"当点击按钮时会调用方法“destroy”

destroy(){
            swal({
                    title: "Delete this comment?",
                    text: "Are you sure? You won't be able to revert this!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#3085d6",
                    confirmButtonText: "Yes, Delete it!",
                    closeOnConfirm: true
                },
                function(){
                    axios.delete('/comment/' + this.comment.id + '/delete');

                    $(this.$el).fadeOut(300, () => {
                        return toastr.success('Comment deleted.');
                    });
                });
        },

i expect when alert come out, if users clicked confirm button then process to delete, but seem like when user clicked the delete function are not executed.我希望当警报出现时,如果用户单击确认按钮然后处理删除,但似乎当用户单击删除 function 时没有执行。 What is the problems here?这里有什么问题?

The this context inside SWAL's callback function is the SWAL instance and not the Vue instance! SWAL的回调函数中的this上下文是SWAL实例,而不是Vue实例! This means that this.comment.id and this.$el are probably undefined and, thus, the function don't execute and would result in a TypeError . 这意味着this.comment.idthis.$el可能undefined ,因此该函数无法执行,并会导致TypeError

To resolve this, 为了解决这个问题,

  • Use Arrow Functions 使用箭头功能

     swal({ title: "Delete this comment?", text: "Are you sure? You won't be able to revert this!", type: "warning", showCancelButton: true, confirmButtonColor: "#3085d6", confirmButtonText: "Yes, Delete it!", closeOnConfirm: true }, () => { axios.delete('/comment/' + this.comment.id + '/delete'); $(this.$el).fadeOut(300, () => toastr.success('Comment deleted.')); }) 
  • Capture this 捕捉this

     let inst = this; swal({ title: "Delete this comment?", text: "Are you sure? You won't be able to revert this!", type: "warning", showCancelButton: true, confirmButtonColor: "#3085d6", confirmButtonText: "Yes, Delete it!", closeOnConfirm: true }, function onDelete(){ axios.delete('/comment/' + inst.comment.id + '/delete'); $(inst.$el).fadeOut(300, () => toastr.success('Comment deleted.')); }) 

This is not only a this context error.这不仅是 this 上下文错误。 SweetAlert is promise-based, so you need to call your axios request, in a Promise.prototype.then() method. SweetAlert 是基于承诺的,因此您需要在 Promise.prototype.then() 方法中调用您的 axios 请求。

swal({
  title: "Delete this comment?",
  text: "Are you sure? You won't be able to revert this!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#3085d6",
  confirmButtonText: "Yes, Delete it!",
  closeOnConfirm: true
})
.then( () => {
  axios.delete('/comment/' + this.comment.id + '/delete');
  $(this.$el).fadeOut(300, () => toastr.success('Comment deleted.'));
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM