vue中封装axios方法
axios基本配置 使用方法
import axios from 'axios' // 创建axios实例
const service = axios.create({
baseURL: process.env.BASE_API, // node环境的不同,对应不同的baseURL
timeout: 5000, // 请求的超时时间
//设置默认请求头,使post请求发送的是formdata格式数据// axios的header默认的Content-Type好像是'application/json;charset=UTF-8',我的项目都是用json格式传输,如果需要更改的话,可以用这种方式修改
// headers: {
// "Content-Type": "application/x-www-form-urlencoded"
// },
withCredentials: true // 允许携带cookie
})
封装get和post方法
import axios from 'axios';
const serverconfig = require('../../static/serverconfig.json') // 这个json文件中配置接口根目录地址 class Axios{
getUrl(url){
return `${serverconfig.ApiUrl}${url}`; // 获取完整的接口地址
}; // post 请求
postServer(opt) {
const _axios = axios.create({
timeout: 10000
});
let data = {};
if (opt.data) {
data = opt.data;
}
_axios.post(opt.url, data).then((response) => {
console.log(response);
if(response.data.status === 'error'){
// 这里用layer弹层插件
layer.open({
content: 'error:' + response.data.hotelInfo
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
if (opt.onFailed) {
opt.onFailed(response);
}
return;
}
if (opt.onSuccess) {
opt.onSuccess(response);
}
}).catch(error => {
if (opt.onFailed) {
opt.onFailed(error);
}
if (!error.response.data.success) {
alert(error.response.data.error.message);
// return;
} });
} // get 请求
getServer(opt) {
const _axios = axios.create({
timeout:10000
});
let data = {};
if (opt.data) {
data = opt.data;
}
_axios.get(opt.url, {params: data}).then((response) => {
if (opt.onSuccess) {
opt.onSuccess(response);
}
}).catch(error => {
if (opt.onFailed) {
opt.onFailed(error);
}
});
} setData(opt) {
let data = {};
if (opt.data) {
data = opt.data;
}
return data;
} } export default Axios;
封装接口
hotel.service.js
import Axios from './axios.service'
const AxiosMethods = new Axios();
sendQueryServer(opt){
const data = AxiosMethods .setData(opt);
const url = AxiosMethods .getUrl('/Home/Query');
AxiosMethods .postServer({url, data, onSuccess: opt.onSuccess,
onFailed: opt.onFailed});
}
}
页面调用query.vue
import HotelServer from "@/service/hotel.service"
const hotelServer = new HotelServer();
methods:{
_sendQueryServer() {
const loadingIndex = this.loadingShow()
hotelServer.sendQueryServer({
onSuccess: (res) => {
layer.close(loadingIndex)
console.log(res)
},
onFailed: (res) => {
layer.close(loadingIndex)
}
})
}
vue中封装axios方法的更多相关文章
- Vue中封装axios
参考: https://www.jianshu.com/p/7a9fbcbb1114 https://www.cnblogs.com/dreamcc/p/10752604.html 一.安装axios ...
- vue中封装公共方法,全局使用
1.以封装的合并单元格为例,首先建立一个util.js 2.在main.js中引用 3.直接在使用该方法的地方调用就可以了
- Vue中封装axios组件实例
首先要创建一个网络模块network文件夹 里面要写封装好的几个组件 在config.js里面这样写 在index.js要这样写 core.js文件里面内容如下 然后要在main.js文件里面要设置 ...
- vue中采用axios发送请求及拦截器
这几天在使用vue中axios发送get请求的时候很顺手,但是在发送post请求的时候老是在成功的回调函数里边返回参数不存在,当时就纳闷了,经过查阅资料,终于得到了解决方案,在此做一总结: 首先我们在 ...
- vue中使用axios进行http通信
1.安装 npm install axios 2.在main.js中全局注册 // axios不可以通过use引入,可以通过修改vue原型链 import axios from 'axios' Vue ...
- vue中代理实现方法
vue中代理实现方法如下: const path = require('path'); function resolve(dir) { return path.join(__dirname, dir) ...
- vue中使用axios与axios的请求响应拦截
VUE中使用Axios axios的安装 npm install axios vue-axios axios在vue的配置与使用 在main.js中引入axios和vue-axios import a ...
- vue中对axios进行封装
在刚结束的项目中对axios进行了实践(好不容易碰上一个不是jsonp的项目), 以下为在项目中对axios的封装,仅封装了post方法,因为项目中只用到了post,如有需要请自行进行修改 src/c ...
- vue中的axios封装
import axios from 'axios'; import { Message } from 'element-ui'; axios.defaults.timeout = 5000;axios ...
随机推荐
- jQuery客户端分页
01 <script src="/js/jquery-1.4.1.js" type="text/javascript"></script> ...
- 关于Unity中UI中的RawImage节点以及制作地图滚动效果
一.贴图的Texture Type属性类型 Texture:会把贴图的大小转换为最相近的2的n次方,比如400X1369会转换为512X1024. Sprite 2D:是贴图的原始大小. 二.RawI ...
- mips cfe命令
设置串口参数 setenv -p LINUX_CMDLINE "console=ttyS0,115200 root=mtd4 rw rootfstype=jffs2" 内核启动参数 ...
- e684. 以多种格式打印
A Book object is used when printing pages with different page formats. This example prints the first ...
- 如今在 Internet 上流传的“真正”的程序员据说是这样的
如今在 Internet 上流传的“真正”的程序员据说是这样的: (1) 真正的程序员没有进度表,只有讨好领导的马屁精才有进度表,真正的程序员会让 领导提心吊胆. (2) 真正的程序员不写使用说明书, ...
- 二分求幂,快速求解a的b次幂
一个引子 如何求得a的b次幂呢,那还不简单,一个for循环就可以实现! void main(void) { int a, b; ; cin >> a >> b; ; i < ...
- Xcode 5.0 编译低版本app
Xcode 5.0 默认的编译环境是iOS7,编译出来的app,安装到iOS7.0版本以上的手机上,会表现出iOS7.0的风格.兼容不太好的应用,布局上可能会因此乱八七糟. 如果还不想让app升级到i ...
- js事件总结
事件冒泡: 什么是事件冒泡,就是最深dom节点触发事件,然后逐级向最外层触发事件.打个比方一棵dom tree:li<ul<div每级都有事件绑定,然后我们触发li的事件,这时ul上的事件 ...
- centos7下git的使用和配置
1.下载git,使用命令: yum install git 2.配置git: git config --global user.name "Your Name" git confi ...
- c# 实现javascript中的escape和unescape
我们在JS经常使用escape和unescape,其实C#也可以的. string s = "中文好伟大的"; Console.Write(Microsoft.JScript.Gl ...