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>

最新文章

  1. 电脑运行msi安装包提示the error code is 2503/2502如何解决
  2. SVN使用说明
  3. angularjs ng-option ie issue解决方案
  4. zabbix监控模式、分布式、自动化
  5. 让wego微购购物分享系统采集拍拍数据功能之腾讯paipai功能采集插件
  6. HDFS-RAID原理和实现
  7. 从千分位格式化谈JS性能优化
  8. [Effective C++ --015]在资源管理类中提供对原始资源的访问
  9. CGLIB学习笔记
  10. poj3080Blue Jeans(在m个串中找到这m个串的 最长连续公共子序列)
  11. java中instanceof的用法
  12. 1164: 零起点学算法71——C语言合法标识符(存在问题)
  13. Chapter 4 Invitations——25
  14. Element-ui 更新tableData 中 row的某一个属性时,没有更新视图的问题
  15. MySql 在cmd下的学习笔记 —— 有关储存过程的操作(procedure)
  16. 对字符串md5加密
  17. git保存用户名和密码
  18. MT【37】二次函数与整系数有关的题
  19. 简易ATM机
  20. Excel VBA语句集

热门文章

  1. redis 入门教程
  2. MyBatis 示例-动态 SQL
  3. Python利用PIL将数值矩阵转化为图像
  4. ORACLE通过JOB定时创建序列
  5. C# Redis分布式锁的应用 - 叶子栈 - SegmentFault 思否
  6. Css解决表格超出部分用省略号显示
  7. Oracle 分页语句
  8. 统一用户认证系统CUAS实现要点
  9. iphone SprintBoard部分私有API总结(不支持iOS8)
  10. MySQL存储引擎MyISAM和InnoDB,索引结构优缺点