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 ...
随机推荐
- 更改HDFS权限
hdfs dfs -chmod -R 755 / 之前执行过这条语句,但是总是提示: 15/05/21 08:10:18 WARN util.NativeCodeLoader: Unable to l ...
- JavaScript 学习笔记(三)
本章学习内容: 1.数组的使用 2.类和对象细节. 3.this关键字的使用 4.构造函数,成员函数的使用 1.数组的使用 在任何的语言中,必须要有的就是数组了,有了数组,使得很多操作都变得非常的 ...
- [转]MySQL远程连接ERROR 2003 (HY000):Can't connect to MySQL server on'XXXXX'(111) 的问题
问题描述: 从一台Linux远程连接另一台linux上的MySQL, 出现ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.x ...
- Linux Source命令及脚本的执行方式解析(转)
当我修改了/etc/profile文件,我想让它立刻生效,而不用重新登录:这时就想到用source命令,如:source /etc/profile对source进行了学习,并且用它与sh 执行脚本进行 ...
- CentOS系统资源常用命令
系统: # uname -a # 查看内核/操作系统/CPU信息 # cat /etc/issue # cat /etc/redhat-release # 查看操作系统版本 # cat /proc ...
- CentOS7.2内核版本查看简述
1.uname 命令 [root@bogon /]# uname --help 用法:uname [选项]... 输出一组系统信息.如果不跟随选项,则视为只附加-s 选项. -a, --all以如 ...
- asp.net mvc中用angularJs写的增删改查的demo。初学者,求指点。。
直接给个代码下载链接.... http://pan.baidu.com/s/1FfVgq 本人刚刚学习angularJs,感觉双向数据绑定蛮爽的... 之前的代码存在点问题,已修复
- mysql获取外键, 根据数据库名和表名获取表所对应的所有外键
SELECT ii.`COLUMN_NAME` FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS ii WHERE ii.`CONSTRAINT_SCHEMA`= ...
- Page与Loaded
When navigate to page, loaded event will be triggered. Back to page, loaded event will be triggered ...
- m_pRecordset->Open
结果: