Promise是一个构造函数,本身身上有all、reject、resolve这几个眼熟的方法,原型上有then、catch等一样很眼熟的方法html
很细致的Promise使用详解 本身脑补vue
vue 工程化的项目通常都会将请求函数进行组件化,ios
api 组件以下:axios
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 调用api
getData () { api.fetchData('https://www.apiopen.top/novelApi','get') .then(res=>{ console.log(res.data); },error => { console.log(error) }) }