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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54156534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:21:44  来源:igfitidea点击:

how to create alert confirm box in vue

javascriptvue.jsvuejs2alert

提问by Jazuly

i want to show a dialog box before deleting a files, how i can do it with vue?

我想在删除文件之前显示一个对话框,我如何用 vue 做到这一点?

here what i try

这是我尝试的

my button to delete a file

我的删除文件的按钮

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

and here my delete method

这里是我的删除方法

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

the dialog is showing but whatever i choose it keep delete the file.

对话框正在显示,但无论我选择什么,它都会删除文件。

回答by Renato Souza de Oliveira

Try this

试试这个

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

回答by Jacob Goh

Simply use if(confirm('are you sure?'))inside DeleteUser.

只需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);
        })
},

and remove the onClick

并删除 onClick

Demo https://jsfiddle.net/jacobgoh101/ho86n3mu/4/

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