如何在vue中创建警报确认框

Jaz*_*uly 9 javascript alert vue.js vuejs2

我想在删除文件之前显示一个对话框,我该如何使用vue?

这是我尝试的

我的按钮删除文件

<a href="javascript:;" v-on:click="DeleteUser(artist.id, index)" onClick="return confirm('are you sure?');">Delete</a>
Run Code Online (Sandbox Code Playgroud)

这是我的删除方法

DeleteUser(id, index) {
                axios.delete('/api/artist/'+id)
                .then(resp => {
                    this.artists.data.splice(index, 1);
                })
                .catch(error => {
                    console.log(error);
                })
            },
Run Code Online (Sandbox Code Playgroud)

该对话框显示,但无论我选择它如何删除文件。

Ren*_*ira 19

尝试这个

<a href="javascript:;" v-on:click="DeleteUser(artist.id, index)">Delete</a>

DeleteUser(id, index) {

   if(confirm("Do you really want to delete?")){

                axios.delete('/api/artist/'+id)
                .then(resp => {
                    this.artists.data.splice(index, 1);
                })
                .catch(error => {
                    console.log(error);
                })
   }
},
Run Code Online (Sandbox Code Playgroud)


Jac*_*Goh 7

只需if(confirm('are you sure?'))在内部使用DeleteUser

DeleteUser(id, index) {
    if(confirm('are you sure?'))
        axios.delete('/api/artist/'+id)
          .then(resp => {
          this.artists.data.splice(index, 1);
        })
          .catch(error => {
          console.log(error);
        })
},
Run Code Online (Sandbox Code Playgroud)

并删除 onClick

演示https://jsfiddle.net/jacobgoh101/ho86n3mu/4/