vue中axios获取后端接口数据有时候需要在请求开始时显示loading,请求结束后隐藏loading,这时候到每次调接口时都写上有点繁琐,有时候还会漏写。

这时候axios的拦截器就起了作用,我们可以在发送所有请求之前和操作服务器响应数据之前对这种情况过滤。定义拦截器如下:

import Vue from 'vue'
import axios from 'axios'
import { Indicator } from 'mint-ui'
import { Toast } from 'mint-ui'
import { getBaseUrl } from './util'
axios.defaults.timeout = 30000
axios.defaults.baseURL = getBaseUrl()
axios.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8'
//http request 拦截器
axios.interceptors.request.use(
config => {
Indicator.open({
text: '加载中...',
spinnerType: 'fading-circle'
})
return config
},
err => {
Indicator.close()
Toast({
message: '加载超时',
position: 'middle',
duration: 3000
})
return Promise.reject(err)
}
) //http response 拦截器
axios.interceptors.response.use(
response => {
Indicator.close()
return response
},
error => {
Indicator.close()
return Promise.reject(error)
}
)

页面js引用如下

<template>
<div>
  <!-- <article class="h48">
  <mt-header fixed title="邮箱确认">
    <router-link to="-1" slot="left">
      <mt-button icon="back"></mt-button>
    </router-link>
  </mt-header>
  </article> -->
  <div class="content">
    <div class="mail-info-txt">
      <p>注册邮箱:{{email}}</p>
    </div>
    <div class="mailconfirm_form">
      <div class="fill-in-list">
        <Verifycode ref="vcode" v-model="verifycode" v-on:vcodeguid="handleVcodeguid"></Verifycode>
      </div>
      <mt-button type="primary" size="large" :class={active:isActive} @click="resetpsd" :disabled="isBtnDisable"> 发送到该邮箱 </mt-button>
    </div>
  </div>
</div>
</template>

<script>
import { Toast } from 'mint-ui'
import { MessageBox } from 'mint-ui'
import '../utils/http' //调用拦截器
import { createguid, getStore, getCookie } from '../utils/util'
import axios from 'axios'
import Verifycode from '@/components/verifycode' export default {
data() {
return {
email: '',
verifycode: '',
loginName: '',
isBtnDisable: true,
isActive: false,
imgcode: ''
}
},
//监听verifycode值变化切换按钮能否点击
watch: {
verifycode: function(val) {
if (val) {
this.isBtnDisable = false
this.isActive = true
} else {
this.isBtnDisable = true
this.isActive = false
}
}
},
created: function() {
let userinfo = JSON.parse(getCookie('userInfo'))
this.email = userinfo ? userinfo.email : ''
this.loginName = userinfo ? userinfo.loginName : ''
},
components: {
Verifycode
},
methods: {
handleVcodeguid: function(guid) {
this.captchaRequestId = guid
},
resetpsd: function() {
let vm = this
axios
.post('接口url', {
loginName: this.loginName,
vcode: this.verifycode,
Email: this.email
})
.then(response => {
var data = response.data
if (data.result.returnCode == '0') {
MessageBox.alert('已发送,请注意查收').then(action => {
vm.$router.go(-2)
})
} else {
Toast(data.result.resultMsg)
this.$refs.vcode.getVcode()
}
})
.catch(error => {})
}
}
}
</script>

axios拦截器使用方法的更多相关文章

  1. axios拦截器的使用方法

    很多时候我们需要在发送请求和响应数据的时候做一些页面处理,比如在请求服务器之前先判断以下用户是登录(通过token判断),或者设置请求头header,或者在请求到数据之前页面显示loading等等,还 ...

  2. Vue2学习小记-给Vue2路由导航钩子和axios拦截器做个封装

    1.写在前面 最近在学习Vue2,遇到有些页面请求数据需要用户登录权限.服务器响应不符预期的问题,但是总不能每个页面都做单独处理吧,于是想到axios提供了拦截器这个好东西,再于是就出现了本文. 2. ...

  3. vue axios拦截器 + 自编写插件 实现全局 loading 效果;

    项目需求:用自定义的 .gif 图标实现全局 loading 效果:为避免在每个页面手动添加,且简单高效的实现,经查阅资料,最终采用了 vue axios拦截器 + 自编写 loading 插件:下面 ...

  4. vue导航守卫和axios拦截器的区别

    在Vue项目中,有两种用户登录状态判断并处理的情况,分别为:导航守卫和axios拦截器. 一.什么是导航守卫? vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.(在路由跳转时 ...

  5. 12. 前后端联调 + ( proxy代理 ) + ( axios拦截器 ) + ( css Modules模块化方案 ) + ( css-loader ) + ( 非路由组件如何使用history ) + ( bodyParser,cookieParser中间件 ) + ( utility MD5加密库 ) + ( nodemon自动重启node ) + +

    (1) proxy 前端的端口在:localhost:3000后端的端口在:localhost:1234所以要在webpack中配置proxy选项 (proxy是代理的意思) 在package.jso ...

  6. AOP 貌似是拦截器 对方法进行拦截

    AOP 貌似是拦截器 对方法进行拦截

  7. axios拦截器搭配token使用

    在了解到cookie.session.token的作用后学习token的使用 cookie是随着url将参数发送到后台,安全性最低,并且大小受限,不超过4kb左右,它的数据保存在客户端 session ...

  8. Vue基于vuex、axios拦截器实现loading效果及axios的安装配置

    准备 利用vue-cli脚手架创建项目 进入项目安装vuex.axios(npm install vuex,npm install axios) axios配置 项目中安装axios模块(npm in ...

  9. Axios拦截器配置

    Axios 拦截器的配置如下 分三块:基础配置.请求之前拦截.响应之前拦截 发送所有请求之前和操作服务器响应数据之前对这种情况过滤. http request 请求拦截器 每次发送请求之前判断是否存在 ...

随机推荐

  1. MySQL SELECT语法(三)JOIN语法详解

    源自MySQL 5.7 官方手册:13.2.9.2 JOIN Syntax SELECT select_expr From table_references JOIN... WHERE... 如上所示 ...

  2. HTML form表单中action的正确写法

    我的Java Web Application的context是myweb,即http://localhost:8080/myweb/index.jsp是欢迎页. 现在我的一个Controller的映射 ...

  3. Redission

    https://github.com/redisson/redisson/wiki/6.-%E5%88%86%E5%B8%83%E5%BC%8F%E5%AF%B9%E8%B1%A1#61-%E9%80 ...

  4. js重点——作用域——作用域分类(三)

    一.作用域可以分为全局作用域,局部作用域(函数作用域)和块级作用域. 1.全局作用域 代码在程序中的任何位置都能被访问到,window对象的内置属性都拥有全局作用域. <script> v ...

  5. 设置自己的bat运行文件-自己用,随时扩展

    start "" "E:\SEST_H5" start "" "C:\Program Files\Sublime Text 3\s ...

  6. 如何上传HTML5应用到SAP云平台的Cloud Foundry环境下

    先使用WebIDE创建一个HTML5应用.New->Project from Template: 从可选模板里选择SAPUI5 Application: 创建一个HTML5 Module,取名为 ...

  7. 关于rtos中任务切换时的程序流程

    今天和一个小伙伴讨论了一下基于cortex-m3内核的RTOS在任务切换时的程序流程,小伙伴说国内某搜索引擎都搜不到这类的信息,所以我才打算写下来,硬件平台是stm32f1​. 这里的切换有两种情况: ...

  8. 算法---FaceNet在Tf下的实战篇

    FaceNet---Tensorflow下的下的实战篇 @WP20190225 ===============目录=============== 一.FaceNet算法简介 二.FaceNet配置与使 ...

  9. Codeforces 1175F The Number of Subpermutations

    做法①:RMQ(预处理NLOGN+后续跳跃蜜汁复杂度) 满足题意的区间的条件转换: 1.长度为R-L+1则最大值也为R-L+1 2.区间内的数不重复 当RMQ(L,R)!=R-L+1时 因为已经保证了 ...

  10. MyBatis-05-解决属性名和字段名不一致的问题

    5.解决属性名和字段名不一致的问题 1.问题 数据库中的字段 新建一个项目,拷贝之前的,测试实体类字段不一致的情况. public class User { private int id; priva ...