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. Hive SQL执行流程分析

    转自 http://www.tuicool.com/articles/qyUzQj 最近在研究Impala,还是先回顾下Hive的SQL执行流程吧. Hive有三种用户接口: cli (Command ...

  2. 仿教程 小爬虫抓取imooc数据

    慕课网的nodejs教程:http://www.imooc.com/learn/348 这人讲的很赞,特别是在事件驱动这点上,耳目一新. 视频略老,所以demo有些过时了,摸索着写了一个自己的小程序. ...

  3. C API 连接MySQL及批量插入

    CMySQLMgr.h: #ifndef _CMYSQLMGR_H_ #define _CMYSQLMGR_H_ #include <iostream> #include "my ...

  4. 不可在 for 循环体内修改循环变量,防止 for 循环失去控制

    不可在 for 循环体内修改循环变量,防止 for 循环失去控制. #include <iostream> /* run this program using the console pa ...

  5. MVC下载远程文件流(WebClient)

    public ActionResult DownLoad_File() { return File(ScLiu(PathUrl), "application/octet-stream&quo ...

  6. Qt 定时器Timer使用

    From: http://dragoon666.blog.163.com/blog/static/107009194201092602326598/ 1.新建Gui工程,在主界面上添加一个标签labe ...

  7. Bash 脚本 getopts为什么最后一个參数取不到

    看以下的Bash脚本: #!/bin/bash interval=0 count=0 pid="" while getopts "p:d:n" arg do c ...

  8. jQuery语法小结(超实用)

    1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...

  9. linux中sftp默认登录的端口号是多少? sftp通过指定的端口号连接?sftp默认端口号

    需求描述: 今天一个同事,遇到个问题,程序连接sftp服务器连接不上,问我端口号是多少, 我想了一下是21还是22,所以就做了测试,发现sftp默认的连接端口号是22, 在此做下记录. 操作过程: 1 ...

  10. 利用Sharepoint 创建轻量型应用之基本功能配置!

    博客同步课程.假设你想跟着视频学习,请跟着例如以下视频: http://edu.csdn.net/course/detail/2097 1.   点击安装程序,出现的界面先期安装完毕准备工具,准备工具 ...