VUE- 異步等待方法嵌套


VUE- 異步等待方法嵌套

 

 

vue在一個方法執行完后執行另一個方法
用Promise來實現。
Promise是ES6的新特性,用於處理異步操作邏輯,用過給Promise添加then和catch函數,處理成功和失敗的情況

ES7中新提出async搭配await,建議使用async搭配await。
使用方法:async/await使用方法 

 

1. Promise的使用方法

//定義方法 
 testFunc()
  { 
      return new Promise((resolve, reject) => {
        this.$axios.get('http://localhost/MKCurtain/logsService/HLogs.ashx?function=GetFilesByPath&filePath=D:/WebSite/MKCurtainJoin/logs',{})
          .then(res => {resolve(res.data);})
          .catch(function(error) {reject(error.message);});
      });
  }
 
 
//方法調用
this.testFunc().then(
  res=>{ console.log(res);},
  error=>{ console.log(error);}
);

 

 

 

2. async/await使用方法

 

async getScheduleList(selectDate) {
    let response;
    // 這里request為向服務的發請求的方法
    await request(api.getScheduleList, {
        date: selectDate
    }).then(res => {
        response = res;
    });
    return response
}

init() {
    this.getScheduleList(selectDate).then(res => {
        console.log(res)
    })
} 

 

 

swichMenu: async function() {
    //點擊其中一個 menu
    const num = await  getNum()
    return num
}

swichMenu().then(res => {
    console.log(res)
})

 

 

 

 

引用:https://blog.csdn.net/superlover_/article/details/79715202


免責聲明!

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



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