axios拦截器使用方法
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拦截器使用方法的更多相关文章
- axios拦截器的使用方法
很多时候我们需要在发送请求和响应数据的时候做一些页面处理,比如在请求服务器之前先判断以下用户是登录(通过token判断),或者设置请求头header,或者在请求到数据之前页面显示loading等等,还 ...
- Vue2学习小记-给Vue2路由导航钩子和axios拦截器做个封装
1.写在前面 最近在学习Vue2,遇到有些页面请求数据需要用户登录权限.服务器响应不符预期的问题,但是总不能每个页面都做单独处理吧,于是想到axios提供了拦截器这个好东西,再于是就出现了本文. 2. ...
- vue axios拦截器 + 自编写插件 实现全局 loading 效果;
项目需求:用自定义的 .gif 图标实现全局 loading 效果:为避免在每个页面手动添加,且简单高效的实现,经查阅资料,最终采用了 vue axios拦截器 + 自编写 loading 插件:下面 ...
- vue导航守卫和axios拦截器的区别
在Vue项目中,有两种用户登录状态判断并处理的情况,分别为:导航守卫和axios拦截器. 一.什么是导航守卫? vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.(在路由跳转时 ...
- 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 ...
- AOP 貌似是拦截器 对方法进行拦截
AOP 貌似是拦截器 对方法进行拦截
- axios拦截器搭配token使用
在了解到cookie.session.token的作用后学习token的使用 cookie是随着url将参数发送到后台,安全性最低,并且大小受限,不超过4kb左右,它的数据保存在客户端 session ...
- Vue基于vuex、axios拦截器实现loading效果及axios的安装配置
准备 利用vue-cli脚手架创建项目 进入项目安装vuex.axios(npm install vuex,npm install axios) axios配置 项目中安装axios模块(npm in ...
- Axios拦截器配置
Axios 拦截器的配置如下 分三块:基础配置.请求之前拦截.响应之前拦截 发送所有请求之前和操作服务器响应数据之前对这种情况过滤. http request 请求拦截器 每次发送请求之前判断是否存在 ...
随机推荐
- MySQL 并发事务问题以及事务的隔离级别
一.并发事务处理带来的问题 相对于串行处理,并发事务(InnoDB)处理能大大增加数据库资源的利用率,提高数据库系统的事务吞吐量,从而可以支持更多用户. 但并发事务处理也会带来一些问题,主要有一下几种 ...
- spring——自动装配
语法:<bean id="..." class="..." autowire="byType"/> autowire属性取值如下 ...
- 尝试 javascript 一对多 双向绑定器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- virtual和override
偶然间看到的题,借此记录. class Program { static void Main(string[] args) { D d = new D(); //第一个D是申明类,第二个D是实例类 A ...
- 为Vim 添加vimgdb支持
为Vim 添加vimgdb支持 1. 下载最新的vim74的源码包 wget ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2 2.下载vimgdb- ...
- Javascript中new的作用
关于js中new关键字的理解,先来看个例子:像这样创建实例时使用new与不使用new有什么区别????function ParasiticPerson(name, age, job) { var ...
- canvas之五角星的绘制
<html> <head> <meta charset=utf-8> <title>绘制简单图形线及矩形</title> <style ...
- Java获取近7个月的起止时间
话不多说,直接上代码 public class Test { @org.junit.Test public void tets() { SimpleDateFormat format = new Si ...
- 13.MySQL锁机制
锁的分类 从对数据的类型 (读\写)分: 1.读锁(共享锁):针对同一份数据,多个读操作可以同时进行而不会互相影响 2.写锁(排它锁):当前写操作没有完成前,它会阻断其他写锁和读锁 从对数据操作的粒度 ...
- VIM从原理上认识^M问题
问题背景 VIM在打开文件的时候如果遇到两种换行符风格(dos与unix)共存的文件,通常会在行尾显示出烦人的^M.如果^M较少,比较容易定位到哪几行出了问题,但是如果^M较多,就很难搞.下面先给出解 ...