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(() => {});
    }
原文地址:https://www.cnblogs.com/my466879168/p/13175282.html