VUE2中axios的使用方法
一,安装
npm install axios
二,在http.js中引入
axios.interceptors.request.use(
config => {
// const token = getCookie('名称');注意使用的时候需要引入cookie方法,推荐js-cookie
//config.data = JSON.stringify(config.data);
let token = localStorage.token;
//let token = 'xxx';
let appid = 'xxx';
let appID = decodeURIComponent(appid);
config.headers.token = token;
//config.headers['Content-Type'] = 'application/x-www-form-urlencoded'; if (config.method == 'get') {
config.params.app_id = appID
config.params.token = token
}
if (config.method == 'post') {
config.data.token = token
config.data.app_id = appID
} return config;
},
error => {
return Promise.reject(err);
}
);
四,封装get方法
export function get(url, params = {}) {
return new Promise((resolve, reject) => {
axios.get(url, {
params: params
})
.then(response => {
resolve(response.data);
})
.catch(err => {
reject(err)
})
})
}
五,封装post方法
export function post(url, params = {}) {
return new Promise((resolve, reject) => {
axios.post(url, params)
.then(response => {
resolve(response.data);
}, err => {
reject(err)
})
})
}
六,封装patch请求
export function patch(url, data = {}) {
return new Promise((resolve, reject) => {
axios.patch(url, data)
.then(response => {
resolve(response.data);
}, err => {
reject(err)
})
})
}
七,封装put请求
export function put(url, data = {}) {
return new Promise((resolve, reject) => {
axios.put(url, data)
.then(response => {
resolve(response.data);
}, err => {
reject(err)
})
})
}
八,实现跨域
const downloadUrl = url => {
let iframe = document.createElement('iframe')
iframe.style.display = 'none'
iframe.src = url
iframe.onload = function() {
document.body.removeChild(iframe)
}
document.body.appendChild(iframe)
}
九,方法的使用
在main.js里引入http.js
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import {get, post } from '@/utils/http' Vue.prototype.$ajax = axios;
Vue.prototype.$post = post;
Vue.prototype.$get = get;
在需要调用的地方
this.$get(url).then((response) => {
}
})
this.$post(url, data).then((response) => {
}
})
this.$put(url, data).then((response) => {
}
})
this.$patch(url, data).then((response) => {
}
})
VUE2中axios的使用方法的更多相关文章
- vue2.0 element-ui中el-upload的before-upload方法返回false时submit()不生效解决方法
我要实现的功能是在上传文件之前校验是否表格中存在重复的数据,有的话,需要弹窗提示是否覆盖,确认之后继续上传,取消之后,就不再上传. 项目中用的element-ui是V1.4.3 <el-uplo ...
- Vue2中实现微信分享支付功能
Vue2中实现微信分享支付功能 近期做了一个微信公众号前后分离项目,前端采用Vue2开发,后端SpringBoot,今天迫不及待的来给大家分享一下这次在开发中遇到的一些坑以及解决办法. 在这里,一些 ...
- 如何在Vue2中实现组件props双向绑定
Vue学习笔记-3 前言 Vue 2.x相比较Vue 1.x而言,升级变化除了实现了Virtual-Dom以外,给使用者最大不适就是移除的组件的props的双向绑定功能. 以往在Vue1.x中利用pr ...
- Vue中axios的使用技巧配置项详解
使用axios首先要下载axios模块包 npm install axios --save 其次需要在使用的文件中引入 import axios from 'axios' 一.调用axios常见两种方 ...
- vue 中axios 的基本配置和基本概念
axios的基本概念及安装配置方法 ajax:异步请求,是一种无需再重新加载整个网页的情况下,能够更新部分网页的技术 axios:用于浏览器和node.js的基于promise的HTTP客户端 a ...
- vue中Axios的封装和API接口的管理
前端小白的声明: 这篇文章为转载:主要是为了方便自己查阅学习.如果对原博主造成侵犯,我会立即删除. 转载地址:点击查看 如图,面对一团糟代码的你~~~真的想说,What F~U~C~K!!! 回归正题 ...
- vue中Axios请求豆瓣API数据并展示到Swipe中
vue中Axios请求豆瓣API数据并展示到Swipe中 1.首先是安装Axios: 安装方法cnpm install axios --save 等待npm安装完毕: 2.在main.js中引入axi ...
- axios 的使用方法
axios是一个基于promise的HTTP库, 可以再浏览器和node.js中使用 浏览器的兼容 安装 使用npm安装 npm install axios --save 使用bower安装 bowe ...
- vue中axios的深入使用
如上所示一条简单的请求数据,用到了vue中axios,promise,qs等等 这里我将vue中用到的axios进行了封装方便日后使用 先对工具类进行封装utils/axios.js: // 引入模 ...
随机推荐
- bzoj 2007: [Noi2010]海拔【最小割+dijskstra】
上来就跑3e5的最大流--脑子抽了 很容易看出,每个地方的海拔都是0或1因为再高了没有意义,又,上去下来再上去没有意义,所以最后一定是从s连着一片0,剩下连着t一片1,然后有贡献的就是01交接的那些边 ...
- vant搜索框问题
最近使用vant搜索框时发现,当输入内容点击一次搜索有数据,点击错号把内容去掉再点击搜索,都没进入点击搜索方法. 解决方法:把bindtop改成bindtouchstart <van-searc ...
- JDK1.7 和 jetty配置教程
系统是windows 7 64位版本,32位版本同理,xp系统的自己google设置环境变量 打开设置环境变量窗口,右键计算机->我的电脑,选择属性 点击高级系统设置 选择环境变量 红线为需要设 ...
- Unable to load script from assets 'index.android.bundle'.Make sure your bundle is packaged correctly or you're running a packager server
curl -k 'http://localhost:8081/index.android.bundle?platform=android' > android/app/src/main/asse ...
- 3.KPCR
KPCR: CPU控制区(Processor Control Region) 当线程进入0环时, FS:[0]指向KPCR(3环时FS[0]-> TEB)每个CPU都有一个KPCR结构体(一个核 ...
- Info.plist配置相关文件访问权限
<key>NSAppleMusicUsageDescription</key> <string>App需要您的同意,才能访问媒体资料库</string> ...
- Verify the Developer App certificate for youraccount is trusted on your device
运行时报错-Verify the Developer App certificate for youraccountis trusted on your device. Open Settings ...
- [題解]luogu_P2055假期的宿舍(二分圖最大匹配)
雖然是裸題但是仍然沒有看出來...... 1.每個人都對應一張床(可以的話),這樣把人和床看成點,對應關係就是邊,跑最大匹配看匹配數量夠不夠即可 2.連邊條件:如果一個學生且不回家,那麼他可以睡自己的 ...
- POJ-1062-昂贵的聘礼(枚举)
链接:https://vjudge.net/problem/POJ-1062 题意: 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为 ...
- linux 的iptables失效解决方法
1.首先查看iptables配置文件:cat /etc/sysconfig/iptables 2.然后查看 iptables 配置文件是否生效:iptables -L,结果如下,很显然和上面的配置 ...