$confirm then()中的异步请求

$confirm then中的异步请求

  • 在$confirm中需要点击确定之后再执行某些操作,最开始我是直接写在methods的方法前面,但是直接报错Can not use keyword ‘await’ outside an async function(不能在异步函数外使用关键字’await’),所以判断await所在的区域不是异步函数
async handleDelete(index, row) {//删除
    this.$confirm('此操作将永久删除该传输记录, 是否继续?', '提示', {
         confirmButtonText: '确定',
         cancelButtonText: '取消',
         type: 'warning'
     }).then(() => {
         let del = await delTranAxios(row.id);
         if(del.data=="删除成功"){
             this.$message({
                 type: 'success',
                 message: '删除成功!'
             });
             this.getTransmission();
         }else{
             this.$message({
                 type: 'error',
                 message: '删除失败!'
             }); 
         }
     }).catch(() => {
         this.$message({
             type: 'info',
             message: '已取消删除'
         });          
     });
 },
  • 正确写法,将async放到then()里面就好
handleDelete(index, row) {//删除
    this.$confirm('此操作将永久删除该传输记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(async() => {
        let del = await delTranAxios(row.id);
        if(del.data=="删除成功"){
            this.$message({
                type: 'success',
                message: '删除成功!'
            });
            this.getTransmission();
        }else{
            this.$message({
                type: 'error',
                message: '删除失败!'
            }); 
        }
    }).catch(() => {
        this.$message({
            type: 'info',
            message: '已取消删除'
        });          
    });
},

版权声明:本文为qq_39040192原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。