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方法的更多相关文章

  1. Vue中封装axios

    参考: https://www.jianshu.com/p/7a9fbcbb1114 https://www.cnblogs.com/dreamcc/p/10752604.html 一.安装axios ...

  2. vue中封装公共方法,全局使用

    1.以封装的合并单元格为例,首先建立一个util.js 2.在main.js中引用 3.直接在使用该方法的地方调用就可以了

  3. Vue中封装axios组件实例

    首先要创建一个网络模块network文件夹  里面要写封装好的几个组件 在config.js里面这样写 在index.js要这样写 core.js文件里面内容如下 然后要在main.js文件里面要设置 ...

  4. vue中采用axios发送请求及拦截器

    这几天在使用vue中axios发送get请求的时候很顺手,但是在发送post请求的时候老是在成功的回调函数里边返回参数不存在,当时就纳闷了,经过查阅资料,终于得到了解决方案,在此做一总结: 首先我们在 ...

  5. vue中使用axios进行http通信

    1.安装 npm install axios 2.在main.js中全局注册 // axios不可以通过use引入,可以通过修改vue原型链 import axios from 'axios' Vue ...

  6. vue中代理实现方法

    vue中代理实现方法如下: const path = require('path'); function resolve(dir) { return path.join(__dirname, dir) ...

  7. vue中使用axios与axios的请求响应拦截

    VUE中使用Axios axios的安装 npm install axios vue-axios axios在vue的配置与使用 在main.js中引入axios和vue-axios import a ...

  8. vue中对axios进行封装

    在刚结束的项目中对axios进行了实践(好不容易碰上一个不是jsonp的项目), 以下为在项目中对axios的封装,仅封装了post方法,因为项目中只用到了post,如有需要请自行进行修改 src/c ...

  9. vue中的axios封装

    import axios from 'axios'; import { Message } from 'element-ui'; axios.defaults.timeout = 5000;axios ...

随机推荐

  1. loadrunner -56992

    设置Run-time Settings ,network speed ,use bandwidth为 512: 回放脚本没有报错,5个并发运行场景报如下错误: vuser_init.c(12): Er ...

  2. 关于Cocos2d-x中坐标系的种类和转换

    注意: 当一个节点有一个子节点的时候,如果移动父节点,子节点也会跟着做相应的移动变化,只要被添加到父节点中,子节点就被绑定了,所以子节点的位置,坐标就会被动地变化. 当一个节点有一个子节点的时候,如果 ...

  3. k近邻法(KNN)和KMeans算法

    k近邻算法(KNN): 三要素:k值的选择,距离的度量和分类决策规则 KMeans算法,是一种无监督学习聚类方法: 通过上述过程可以看出,和EM算法非常类似.一个简单例子, k=2: 畸变函数(dis ...

  4. [转] C# mysql 事务回滚

    什么是数据库事务 数据库事务是指作为单个逻辑工作单元执行的一系列操作. 设想网上购物的一次交易,其付款过程至少包括以下几步数据库操作: · 更新客户所购商品的库存信息 · 保存客户付款信息--可能包括 ...

  5. imx6 android5.1 编译

    imx6 android5.1 编译 记录一下编译imx6dl android的命令. Android build cd ~/myandroid source build/envsetup.sh lu ...

  6. e1087. 用For循环做数组的遍历

    The for statement can be used to conveninently iterate over the elements of an array. The general sy ...

  7. php -- php控制linux关机、重启、注销

        php 里面有个 system(exec) 方法, 可以调用系统命令.     重启先建立一个脚本(比喻 /root/reboot_server.sh ),重启用的.//路径可随便,但最少必须 ...

  8. samtools flags 的含义

    对于双端比对的数据,生成的BAM文件中,R1端序列和R2端序列的标识符是一样的,之前一直不知道如何根据bam文件区分哪条序列是R1端,哪条序列是R2端,昨天仔细研究了一下,原来代表R1端和R2端的信息 ...

  9. Gray Code - 格雷码

    基本概念 格雷码是一种准权码,具有一种反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的 ...

  10. Unity中坐标系转换方法

    前言 本篇文章主要是参考<Unity API 解析>---陈泉宏. 这是本人在学校图书馆找到一本书,主要介绍的就是常用的类,比较实用,没有冗余的地方.在此推荐一下这本书! 一.Screen ...