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 }) }
相关推荐
yuzhu 2020-11-16
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
sjcheck 2020-11-03
怪我瞎 2020-10-28
源码zanqunet 2020-10-28
gloria0 2020-10-26
王军强 2020-10-21
学习web前端 2020-09-28
QiaoranC 2020-09-25
anchongnanzi 2020-09-21
安卓猴 2020-09-12
Macuroon 2020-09-11
kiven 2020-09-11
LittleCoder 2020-09-11
Cheetahcubs 2020-09-13
小焊猪web前端 2020-09-10
颤抖吧腿子 2020-09-04
softwear 2020-08-21