在Vue單頁面應用中使用Promise鏈式調用


eg:

  this.commonLoginFun().then((res) => {
      if (res.errNo === 0) {
        const { isLogin } = res.data;
        if (isLogin) {
          this.isLogin = true;
          this.userPhone = res.data.phone;
        } else {
          this.isLogin = false;
        }
      }
    });

在commonLoginFun中:

methods:{

  commonLoginFun(){

    return xxx.axios.xxxx;  //2個return,把異步函數return,把異步函數的返回值return。 

  }

}

總結:

1. 使用then方法之后會返回一個promise對象,可以繼續使用then方法調用,再次調用所獲得的參數是上個then方法return的內容。

2. 將一個promise對象當做參數傳遞給promise.resolve()會被直接返回。

 


免責聲明!

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



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