1、安装axios:

npm install --save axios vue-axios

2、安装qs:

qs.stringify(data)可以解决data数据格式问题

npm install --save axios vue-axios qs

3、在main.js页面中引用:

 import Vue from 'vue'
import axios from 'axios'
import qs from 'qs' Vue.prototype.$http = axios
Vue.prototype.qs = qs

4、在vue中使用

 <script>
export default{
data(){
return {
msg:'axios使用'
}
},
created(){
this.axios({
method:'post',
url:'',
data:this.qs.stringify({
msg:this.msg
})
}).then((response)=>{
console.log(response)
})
}
}
</script>

以上是axios在vue中的简单应用,在实际的项目中,我们还需要考虑请求超时、是否登录等问题,这时需要在http请求中添加拦截器,在请求头中加token

下面是一个axios的工具interceptAxios.js

 //http配置
//引入axios以及element ui 中的loading和message组件
import axios from 'axios'
import store from '../../store/store'
import * as types from '../../store/types'
import router from '../../routes'
import {Loading,Message} from 'element-ui'
//超时时间
axios.defaults.timeout = 500000
//http请求拦截器,在请求头中加token
var loadinginstace
axios.interceptors.request.use(config=>{
if (store.state.token) {
config.headers.Authorization = `${store.state.token}`
}
//element ui Loading方法
//loadinginstace = Loading.service({fullscreen:true})
return config
},error=>{
//loadinginstace.close()
Message.error({
message:'加载超时'
})
return Promise.reject(error)
})
//http响应拦截器
axios.interceptors.response.use(response=>{//响应成功关闭loading
//loadinginstace.close()
return response
},error=>{
if (error.response) {
switch (error.response.status) {
case 401:
// 401 清除token信息并跳转到登录页面
store.commit(types.LOGOUT)
store.commit(delPermission)
console.log("token无效----------------------------------")
// 只有在当前路由不是登录页面才跳转
router.currentRoute.path !== 'login' &&
router.replace({
path: 'login',
query: { redirect: router.currentRoute.path },
})
}
}
//loadinginstace.close()
Message.error({
message:'加载失败'
})
return Promise.reject(error)// 返回接口返回的错误信息
})
export default axios

在main.js中配置:

 import Vue from 'vue'
import axios from './assets/js/interceptAxios'
import VueAxios from 'vue-axios'
import store from './store/store'
import Qs from 'qs' Vue.prototype.HOST="/api"//解决跨域问题,做一个反向代理
// 将axios挂载到prototype上,在组件中可以直接使用this.axios访问
Vue.prototype.$http=axios
Vue.prototype.qs=Qs
Vue.prototype.store = store Vue.use(VueAxios,axios)

在vue中应用:

 <script>
export default {
data(){
     return {
5       msg:''
6     } 
},
methods:{
tool(data){
this.axios.post(this.HOST+'/vueHome/QueryTourOrigin.vue',this.qs.stringify(data))
.then(function(res){
12 console.log(res);
13 })
14 }
15 }
16 }
17 </script>

以上只是些简单的应用,应该还有更深层次的使用,待续......

axios在Vue中的简单应用(一)的更多相关文章

  1. axios在vue中的简单配置与使用

    一.axios 简介 axios 是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端,它本身具有以下特征:https://hzzly.github.io/2017/03/12/ ...

  2. axios 在vue中使用

    下载组件: npm install axios --save npm install qs --save //处理对象防止产生跨域问题 引入: 新建axios文件夹,文件下新建index.js文件 i ...

  3. vue中超简单的方法实现点击一个按钮出现弹框,点击弹框外关闭弹框

    效果图展示: View层 <template> <div> <div class="mask" v-if="showModal" ...

  4. vue中watch简单使用

    watch是一个对象,具有键值对:键指被监听的数据,值指处理方式. 值类型包括以下三个: 第一个handler:其值是一个回调函数.即监听到变化时应该执行的函数. 第二个是deep:其值是true或f ...

  5. vue中简单的小插曲

    我们现在来学习一下vue中一些简单的小东西: 首先我们必须要引入vue.js文件哦! 1.有关文本框里的checkbox js代码: new Vue({ el:"#app", da ...

  6. 微信 jssdk 逻辑在 vue 中的运用

    微信 jssdk 在 vue 中的简单使用 import wx from 'weixin-js-sdk'; wx.config({ debug: true, appId: '', timestamp: ...

  7. vue中axios的简单使用

    我们一般在用jq的时候会使用到ajax来进行与服务器之间的交流,vue中也提供了相应的类似于ajax的方法-axios来进行与服务器之间的数据传递 现在的这篇是最简单的使用,后续会添加上来复杂的使用 ...

  8. vue中axios的封装以及简单使用

    一.axios的封装 在vue中为了使用axios使用方便,不需要每一个模块进行导入,就需要对其进行封装: 1.新建http.js模块 import axios from 'axios' // 设置基 ...

  9. vue中Axios的封装和API接口的管理

    前端小白的声明: 这篇文章为转载:主要是为了方便自己查阅学习.如果对原博主造成侵犯,我会立即删除. 转载地址:点击查看 如图,面对一团糟代码的你~~~真的想说,What F~U~C~K!!! 回归正题 ...

随机推荐

  1. 【踩坑记录】 使用form标签的 reset() 方法报错原因及处理方法

    如果form标签内包含了 id 为 reset 的元素,在调用form的 reset() 方法时,会报xxx.reset is not a function,原因是在调用form的 reset() 方 ...

  2. Spark RDD初探(一)

    本文概要 本文主要从以下几点阐述RDD,了解RDD 什么是RDD? 两种RDD创建方式 向给spark传递函数Passing Functions to Spark 两种操作之转换Transformat ...

  3. 小程序是单页面应用,有一个页面执行wx.showLoading(),其他页面也会显示

    my.js onLoad: function (options) { setTimeout(res=>{ wx.showLoading({ title: '10s后出现', }) }, ) }, ...

  4. 题解 【POJ1952】 BUY LOW, BUY LOWER

    题目意思: 给你一个长度为\(n\)(\(1<=n<=5000\))的序列,并求出最长下降子序列的长度及个数, 并且,如果两个序列中元素的权值完全相同,那么即使它们的位置不一样,也只算一种 ...

  5. Linux 目录共享

    ## 安装 nfs 和 rpc yum install -y nfs-utils rpcbind ## ubuntu 安装 nfs 和 rpc ## apt-get install nfs-kerne ...

  6. jquery toggle()方法 语法

    jquery toggle()方法 语法 作用:toggle() 方法用于绑定两个或多个事件处理器函数,以响应被选元素的轮流的 click 事件.该方法也可用于切换被选元素的 hide() 与 sho ...

  7. .net文件夹上传源码

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  8. ngx_http_auth_request_module 第三方认证

    shell > vim /usr/local/nginx-1.10.2/conf/vhost/auth.conf # 这是第三方认证服务器,认证逻辑使用的 PHP 代码 server { lis ...

  9. jQuery文档操作之克隆操作

    语法: $(selector).clone(); 解释:克隆匹配的DOM元素 $("button").click(function(event) { //1.clone():克隆匹 ...

  10. Latex中 summation前后距离的设置

    use \hspace ,eg., \hspace{-.1cm} before and after summation to stop violation of margin. 比如下面一段公式代码 ...