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. 使用CreateWindowEx创建子窗口的注意事项

    比如: 使用 HWND child = CreateWindowEx(0,L"childclass",NULL,WS_CHILD | WS_VISIBLE | WS_CLIPSIB ...

  2. 基于Kibana的可视化监控报警插件sentinl入门

    sentinl是什么 Kibi/Kibana Alert & Reporting App Watching your data, 24/7/365 sentinl是一个免费的kibana预警与 ...

  3. Pandas中DataFrame数据合并、连接(concat、merge、join)之merge

    二.merge:通过键拼接列 类似于关系型数据库的连接方式,可以根据一个或多个键将不同的DatFrame连接起来. 该函数的典型应用场景是,针对同一个主键存在两张不同字段的表,根据主键整合到一张表里面 ...

  4. Acwing-203-同余方程(扩展欧几里得)

    链接: https://www.acwing.com/problem/content/205/ 题意: 求关于x的同余方程 ax ≡ 1(mod b) 的最小正整数解. 思路: 首先:扩展欧几里得推导 ...

  5. Chrome报错提示Unchecked runtime.lastError: The message port closed before a response was received.

    经过查询,此错误是Chrome扩展插件引起的.由于Chrome修改了API接口,原来的请求被拦截.(Chrome 73 onwards disallows cross-origin requests ...

  6. mysql 导入SQL文件报错

    ----MySQL导入数据的时候,总是报错,本地测试都没办法测试---- 导出SQL文件正常, 在导入SQL文件的时候, 报错-----大部分数据表导入成功, 个别的失败 在网上看到很多建议: 建议别 ...

  7. C Vus the Cossack and Strings ( 异或 思维)

    题意 : 给你两个只包含 0 和 1 的字符串 a, b,定义函数 f ( A, B ) 为 字符串A和字符串B 比较 存在多少个位置 i 使得 A[ i ] != B[ i ] ,例如 f(0011 ...

  8. js 获取 touch length

    window.addEventListener("touchstart", touchHandler, false); function touchHandler(event){ ...

  9. luogu p4141 消失之物(背包dp+容斥原理)

    题目传送门 昨天晚上学长讲了这题,说是什么线段树分治,然后觉得不可做,但那还不是正解,然后感觉好像好难的样子. 由于什么鬼畜的分治不会好打,然后想了一下$O(nm)$的做法,想了好长时间觉得这题好像很 ...

  10. 邻居子系统 之 邻居项创建__neigh_create

    概述 IP层输出数据包会根据路由的下一跳查询邻居项,如果不存在则会调用__neigh_create创建邻居项,然后调用邻居项的output函数进行输出: __neigh_create完成邻居项的创建, ...