如何使用Sweet alert和vue.js提醒和删除任务?

时间:2016-06-26 07:12:43

标签: javascript jquery vue.js

我收到了Sweet Alert的确认提醒消息但是当我点击确认的消息时,我无法删除该任务。

methods: {
    confirmDelete: function(index) { //index is passed by the button
        //var self = this;
        swal({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!',
            closeOnConfirm: true
        }, function(isConfirm) {
            if (isConfirm) {
                this.contacts.splice(index, 1);
                swal(
                    'Deleted!',
                    'Your file has been deleted.',
                    'success'
                );

                return true;
            } else {
                return false;
            }
        }.bind(this));
    }
}

1 个答案:

答案 0 :(得分:0)

可能问题是this声明 尝试这样的事情:

    var self = this;
    function(isConfirm) {
              if(isConfirm){
                  self.contacts.splice(index, 1);
                  swal(
                    'Deleted!',
                    'Your file has been deleted.',
                    'success'
                  );

                  return true;
              }
              else{
                return false;
              }

工作版本: https://jsfiddle.net/jyfdh32g/

我改变了什么: vue版本持续稳定 - 1.0.25 确认按钮动作样式来自:https://limonte.github.io/sweetalert2/示例。

为什么$index无效?

  

参数顺序更新:(value,index)在arr,(value,key,index)中   obj $ index和$ key已弃用

https://github.com/vuejs/vue/issues/2873