美文网首页
async/await的另一种使用

async/await的另一种使用

作者: 3e2235c61b99 | 来源:发表于2020-11-20 11:12 被阅读0次

之前使用async/await都是在请求接口或者自己写的promise时使用,今天看到个其他用法,记录一下

element的Message Box 组件官网给的用法是下面这样,我也一直是这样用的:

  open() {
    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      this.$message({
        type: 'success',
        message: '删除成功!'
      });
    }).catch(() => {
      this.$message({
        type: 'info',
        message: '已取消删除'
      });          
    });
  }

还可以按下面这样写:

async open() {
    try{
        await this.$confirm('此操作将永久删除该文件, 是否继续?');
        this.$message('删除成功')
    } catch {
        error === 'cancel' ? this.$message('已取消删除') : this.$message.error('删除失败')
    }   
}

这里这样写是因为:调用$confirm方法打开消息提示后,用了 Promise 来处理后续响应

太菜了
好好学习,天天向上

相关文章

网友评论

      本文标题:async/await的另一种使用

      本文链接:https://www.haomeiwen.com/subject/vrldiktx.html