1 Star 1 Fork 0

雨·E/axios-vue

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ajax.js 4.11 KB
一键复制 编辑 原始数据 按行查看 历史
雨·E 提交于 2017-09-06 10:24 . 更新到es7
import axios from 'axios'
import qs from 'qs'
/**
* Created by 白雨浓 on 17-8-10 上午11:29.
*
* ajax axios 封装
*/
const SUCCESS_CODE = 1;
axios.defaults.baseURL = "/api";
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
const Ajax = {
errHandler (Vue, err) {
if (err.response !== undefined)
Vue.prototype.$message.error(`请求错误: ${JSON.stringify(err.response.status)}`);
else
Vue.prototype.$message.error(err);
},
install(Vue){
const $$get = (path, params) => {
if (params !== undefined) {
let b = true;
for (const key in params) {
if (b) {
path += `?${key}=${params[key]}`;
b = !b;
}
else
path += `&${key}=${params[key]}`
}
}
return new Promise((resolve, reject) => {
axios.get(path).then(res => {
resolve([res.data, res]);
}).catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$post = (path, params) => {
return new Promise((resolve, reject) => {
axios.post(path, qs.stringify(params))
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$put = (path, params) => {
return new Promise((resolve, reject) => {
axios.put(path, qs.stringify(params))
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$patch = (path, params) => {
return new Promise((resolve, reject) => {
axios.patch(path, qs.stringify(params))
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$delete = (path, params) => {
if (params !== undefined) {
let b = true;
for (const key in params) {
if (b) {
path += `?${key}=${params[key]}`;
b = !b;
}
else
path += `&${key}=${params[key]}`
}
}
return new Promise((resolve, reject) => {
axios.delete(path).then(res => {
resolve([res.data, res]);
}).catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$postj = (path, json) => {
return new Promise((resolve, reject) => {
axios.post(path, json, {headers: {"Content-Type": "application/json"}})
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$putj = (path, params) => {
return new Promise((resolve, json) => {
axios.put(path, json, {headers: {"Content-Type": "application/json"}})
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
const $$patchj = (path, json) => {
return new Promise((resolve, reject) => {
axios.patch(path, json, {headers: {"Content-Type": "application/json"}})
.then(res => {
resolve([res.data, res]);
})
.catch((err) => {
this.errHandler(Vue, err);
reject(err);
})
})
};
Vue.prototype.$$ajax = axios;
Vue.prototype.$$get = $$get;
Vue.prototype.$$post = $$post;
Vue.prototype.$$put = $$put;
Vue.prototype.$$patch = $$patch;
Vue.prototype.$$delete = $$delete;
Vue.prototype.$$postj = $$postj;
Vue.prototype.$$putj = $$putj;
Vue.prototype.$$patchj = $$patchj;
}
};
export default Ajax;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/findviewbydream/axios-vue.git
git@gitee.com:findviewbydream/axios-vue.git
findviewbydream
axios-vue
axios-vue
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385