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) }) }
