vue中局部封装axios
Vue中局部配置axios
'use strict'
import axios from 'axios';
import {
Loading
} from 'element-ui';
export const http = (config) => {
const instance = axios.create({
baseUrl: '服务器地址',
timeout: '设置过期时间'
})
// 自定义动画函数
let loading;
let startLoading = () => {
/* 开场动画 */
loading = Loading.service({
lock: true,
text: '正在加载...客官请稍等...',
background: 'rgba(0,0,0,.6)'
})
};
let endLoading = () => {
/* 结束动画 */
loading.close()
};
// 设置请求拦截
instance.interceptors.request.use(
function (config) {
// Do something before request is sent
startLoading()
return config
},
function (error) {
// Do something with request error
return Promise.reject(error)
}
)
// 设置响应拦截
instance.interceptors.response.use(
function (response) {
// Do something with response data
endLoading()
return response
},
function (error) {
// Do something with response error
endLoading()
return Promise.reject(error)
}
)
return instance(config)
}
// GET 实例
http({
url: '127.0.0.1:8080/system/category',
method: 'GET',
params: {
data: 'get请求传递的参数'
}
}).then(res => {
console.table(res)
})
.catch(err => {
console.log(err)
})
// POSt 实例
http({
url: '127.0.0.1:8080/system/user',
method: 'POST',
data: {
userName: '',
password: ''
}
}).then(res => {
console.table(res)
})
.catch(err => {
console.log(err)
})
vue中局部封装axios的更多相关文章
- vue中的ajax - axios
vue中的ajax - axios axios - 简书 使用 axios 实现 ajax 方案 VUE 更好的 ajax 上传处理 axios.js vue.js 自2.0版本已经不对 vue-re ...
- Vue 中使用 TypeScript axios 使用方式
Vue 中使用 TypeScript axios 使用方式 方式一 import axios from 'axios'; Vue.prototype.$axios = axios; // 在 .vue ...
- 【转】Vue 2.0封装axios笔记
前言 单页面应用大多采用前后端分离开发思路,我们知道,前端和后端交互有多中方式(服务器端渲染.Ajax.websocket等),今天我们主要讲解Ajax部分. 最近团队讨论了一下,Ajax 本身跟 V ...
- Vue笔记:封装 axios 为插件使用
前言 自从Vue2.0推荐大家使用 axios 开始,axios 被越来越多的人所了解.使用axios发起一个请求对大家来说是比较简单的事情,但是axios没有进行封装复用,项目越来越大,引起的代码冗 ...
- 创建Vue项目及封装axios
1. 始vue化项目 https://www.cnblogs.com/xiaonq/p/11027880.html vue init webpack deaxios # 使用脚手架创建项目 deaxi ...
- vue中局部组件的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue中使用Ajax(axios)、vue函数中this指向问题
Vue.js 2.0 版本推荐使用 axios 来完成 ajax 请求.Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中. axios中文文档库:http ...
- Vue中如何使用axios请求跨域数据
1.axios不支持jsonp,因为axios的作者觉得jsonp不太友好,推荐用CORS方式更为干净: 2.在使用axios发送请求时,服务器端设置 res.header("Access- ...
- 在Vue中如何使用axios跨域访问数据
最近在项目中需要用到axios,所以就恶补一下这个axios到底是什么东东.越来它是vue-resource的替代品,官网也说了,以后都用axios, vue-resource不在维护.那么这个axi ...
随机推荐
- MySQL 关于存储过程的操作
-- 存储过程 --> 一组为了完成特定功能的SQL语句集 -- IN 传入参数 delimiter // create procedure mysql ( IN id int ) begin ...
- qt一些函数
sleep(n); 休息ns msleep(1000); 休息1000毫秒 usleep(1000000); //休息n微妙
- mysql索引分类及实现原理
索引分类:主键索引.唯一索引.普通索引.全文索引.组合索引 1.主键索引:即主索引,根据主键pk_clolum(length)建立索引,不允许重复,不允许空值 ALTER TABLE 'table_n ...
- Vue3.0+TypeScript
序言 资料 https://www.cnblogs.com/chanwahfung/p/11968205.html
- Confluence 6.15 博客页面(Blog Posts)宏
博客页面宏允许你 Confluence 页面中显示博客页面.通过单击博客的标题将会把你链接到博客页面中. 使用博客页面宏 为了将博客页面宏添加到页面中: 从编辑工具栏中,选择 插入(Insert) ...
- sh_20_for语法演练
sh_20_for语法演练 for num in [1, 2, 3]: print(num) if num == 2: break else: # 如果循环体内部使用break退出了循环 # else ...
- vue中使用laydate.js插件
1.到官网下载laydate.js https://www.layui.com/laydate/ 2.下载好后,将包解压好放在index.html同级的地方.我是在public中建立个statick文 ...
- 费马小定理证明 (copy的,自己捋清楚)
费马小定理:假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p) 证明(copy的百度百科,加点自己的解释) 引理1. 若a,b,c为任意3个整数,m为正整数,且(m,c)=1 ...
- TCP->IP输出 之 ip_queue_xmit、ip_build_and_send_pkt、ip_send_unicast_reply
概述 ip_queue_xmit是ip层提供给tcp层发送回调,大多数tcp发送都会使用这个回调,tcp层使用tcp_transmit_skb封装了tcp头之后,调用该函数,该函数提供了路由查找校验. ...
- flask登录功能实现的思路
flask登录实现过程思路:前端创建表单,post方法,userno,pwd参数 后端首先创建登录验证函数check(验证函数是指通过request.get来获取前端的userno,pwd.然后将两个 ...