Promise 配合 axios 使用


Promise是一個構造函數,自己身上有all、reject、resolve這幾個眼熟的方法,原型上有then、catch等同樣很眼熟的方法

很細致的Promise使用詳解 自己腦補

 

vue 工程化的項目一般都會將請求函數進行組件化,

api 組件如下:

export default {
    
    fetchData (url, methods, datas) {
        return new Promise((resolve, reject) => {
          axios({
              url: url,
              method: methods,
              data: datas
            }).then((res) => {
              resolve(res)
            }).catch(function (error) {
              reject(error)
              // console.log(error);
            })
        })
    }
}

index.vue 調用

getData () {
     api.fetchData('https://www.apiopen.top/novelApi','get')
        .then(res=>{
             console.log(res.data);
         },error => {
             console.log(error)
     })
}

 


免責聲明!

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



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