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(() => {});
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM