vue 组件的封装之基于axios的ajax请求方法
更新时间:2018年08月11日 15:30:07 作者:scorpio_h
今天小编就为大家分享一篇vue 组件的封装之基于axios的ajax请求方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
如下所示:
import Vue from 'vue'
let service = {
url: 'http://host.xxxxx.com/xxx.php'
}
service.ajaxReuqest = (url, options, type, fileFlag) => {
for (const i in options) {
if (!options[i] && options[i] !== 0 && (options[i].length && options[i].length > 0)) {
delete options[i]
}
}
let promise = new Promise((resolve, reject) => {
if (fileFlag) {
Vue.axios.post(url, options, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then((res) => {
resolve(res)
})
} else if (type === 'GET') {
Vue.axios.get(url, { params: options }).then((res) => {
resolve(res.data.resultObj)
}).then((err) => {
reject(err)
})
} else {
Vue.axios.post(url, options).then((res) => {
resolve(res)
}).then((err) => {
reject(err)
})
}
})
return promise
}
支持POST GET请求以及图片上传,基于axios,适用于vue,
以异步获取省份列表作为例子:
// 获取省份信息
service.getProvinceList = (options) => {
return service.ajaxRequest(service.url + '/basic/getProvinceList', options, 'POST')
}
getProvinceList () {
service.getProvinceList({}).then((res) => {
this.provinceList = res.data.resultObj.data
})
}
以上这篇vue 组件的封装之基于axios的ajax请求方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Vue路由对象属性 .meta $route.matched详解
今天小编就为大家分享一篇Vue路由对象属性 .meta $route.matched详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2019-11-11
VUE Error: getaddrinfo ENOTFOUND localhost
这篇文章主要介绍了VUE Error: getaddrinfo ENOTFOUND localhost,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-05-05


最新评论