zoukankan      html  css  js  c++  java
  • this.$confirm里面使用await异步调取接口数据

    this.$confirm里面使用await

    在this.$comfirm中需要点击确定后进行某些异步操作,如果在方法名上写async的话会直接报错:Can not use keyword 'await' outside an async function (419:23)

    async cancelappointment(item) {
          this.$confirm("确认取消该议程吗?", "取消", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then( () => {
              let params = {};
              params.meeting_id = item.meeting_id;
              params.user_id = this.$store.state.userId;
              params.agenda_id = item.agenda_id;
              params.agenda_type = item.remind_type;
              let result = await this.$store.dispatch(
                "API_conference/cancelMeetingLive",
                params
              );
              if (result.success) {
                this.list = this.list.filter(item => {
                  item.agenda_id != item.agenda_id;
                });
                this.currentData = this.currentData.filter(item => {
                  item.agenda_id != item.agenda_id;
                });
                if (this.currentData.length == 0) {
                  this.timedata.splice(this.current, 1);
                  this.current = 0;
                  // this.timedata=this.timedata.filter(item=>{
                  //   item!=item.day
                  // })
                }
              }
            })
            .catch(() => {});
        }, 
    

    正确的写法是将async写在then中箭头函数的前面 :

    cancelappointment(item) {
            this.$confirm("确认取消该议程吗?", "取消", {
              confirmButtonText: "确认",
              cancelButtonText: "取消",
              type: "warning"
            })
              .then(async () => {
                let params={}
                params.meeting_id=item.meeting_id
                params.user_id=this.$store.state.userId
                params.agenda_id=item.agenda_id
                params.agenda_type=item.remind_type
                let result=await this.$store.dispatch("API_conference/cancelMeetingLive",params)
                if (result.success){
                  this.list=this.list.filter(item=>{
                    item.agenda_id!=item.agenda_id
                  })
                  this.currentData=this.currentData.filter(item=>{
                    item.agenda_id!=item.agenda_id
                  })
                  if (this.currentData.length == 0){
                    this.timedata.splice(this.current,1)
                    this.current=0
                    // this.timedata=this.timedata.filter(item=>{
                    //   item!=item.day
                    // })
                  }
                }
              })
              .catch(() => {});
        }
    
  • 相关阅读:
    python基础-函数递归
    JS中变量、作用域的本质,定义及使用方法
    JS数据类型和堆栈+变量比较和值的复制+参数传递和类型检测
    宣传页项目开发(三)
    宣传页项目开发(二)
    宣传页项目实战
    CSS Sprite雪碧图应用
    PS切图工具
    根据js轮播图原理写出合理的结构与样式、并实现js交互效果
    JS事件流、DOM事件流、IE事件处理、跨浏览器事件处理、事件对象与类型
  • 原文地址:https://www.cnblogs.com/my466879168/p/13175282.html
Copyright © 2011-2022 走看看