细说vue axios登录请求拦截器

当我们在做接口请求时,比如判断登录超时时候,通常是接口返回一个特定的错误码,那如果我们每个接口都去判断一个耗时耗力,这个时候我们可以用拦截器去进行统一的http请求拦截。

1.安装配置axios


cnpm install --save axios

我们可以建一个js文件来做这个统一的处理,新建一个axios.js,如下


import axios from 'axios'
import { Indicator } from 'mint-ui';
import { Toast } from 'mint-ui';
// http request 拦截器
axios.interceptors.request.use(
  config => {
    Indicator.open()
    return config;
  },
  err => {
    Indicator.close()
    return Promise.reject(err);
  });
// http response 拦截器
axios.interceptors.response.use(
  response => {
    Indicator.close()
    return response;
  },
  error => {
    Indicator.close()
  }); //欢迎加入前端全栈开发交流圈一起吹水聊天学习交流:864305860
export default axios

然后在main.js中引入这个js文件


import axios from './axio';
Vue.prototype.$axios = axios;

这样就可以使用axios去请求了,在组件中可以用this.axios去调用


this.$axios({
    url:requestUrl+'homePage/v1/indexNewPropertiesResult',
    method:'POST',
   }).then(function(response){ //接口返回数据
    console.log(response)
    that.modulesArr=response.data.data.modules;
//   that.getRecommendGoods(0);
   });//欢迎加入前端全栈开发交流圈一起吹水聊天学习交流:864305860

只有用axios请求接口,才能去拦截,现在已经能在axios.js中拦截到了,可以在两个状态中做你需要的操作

补充:

  • axios使用拦截器统一处理所有的http请求
  • axios使用拦截器
  • 在请求或响应被 then 或 catch 处理前拦截它们。

http request拦截器


// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  return config;
 }, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
 });//欢迎加入前端全栈开发交流圈一起吹水聊天学习交流:864305860

http respones拦截器


// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response;
 }, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
 });//欢迎加入前端全栈开发交流圈一起吹水聊天学习交流:864305860

移除拦截器


var myInterceptor = axios.interceptors.request.use(function () {/*...*/});
axios.interceptors.request.eject(myInterceptor);

为自定义axios实例添加拦截器


var instance = axios.create();
instance.interceptors.request.use(function () {/*...*/});

结语

感谢您的观看,如有不足之处,欢迎批评指正。

原文地址:https://segmentfault.com/a/1190000017283733

原文地址:https://www.cnblogs.com/datiangou/p/10121702.html

时间: 2024-08-29 13:01:13

细说vue axios登录请求拦截器的相关文章

Vue添加请求拦截器

一.现象 统一处理错误及配置请求信息 二.解决 1.安装 axios  , 命令: npm install axios --save-dev 2.在根目录的config目录下新建文件 axios.js  ,内容如下: import axios from 'axios' // 配置默认的host,假如你的API host是:http://api.htmlx.clubaxios.defaults.baseURL = 'http://api.htmlx.club' // 添加请求拦截器axios.in

vue 路由拦截器和请求拦截器

vue 拦截器 路由拦截器 已路由为导向 router.beforeEach((to,from,next)=>{ if(to.path=='/login' || localStorage.getItem('token')){ next(); }else{ alert('请重新登录'); next('/login'); } }) 请求拦截器 当发送请求时才会触发此功能 axios.interceptors.request.use(function (config) { let token = wi

Vue+axios 实现http拦截及路由拦截

现如今,每个前端对于Vue都不会陌生,Vue框架是如今最流行的前端框架之一,其势头直追react.最近我用vue做了一个项目,下面便是我从中取得的一点收获. 基于现在用vue+webpack搭建项目的文档已经有很多了,我就不再累述了. 技术栈 vue2.0 vue-router axios 拦截器 首先我们要明白设置拦截器的目的是什么,当我们需要统一处理http请求和响应时我们通过设置拦截器处理方便很多. 这个项目我引入了element ui框架,所以我是结合element中loading和me

spring登录验证拦截器和根据用户角色登录

大家都知道spring的用户登录拦截器,确实省去了程序员不少的精力,下面说说我在项目中使用的感受. 德安微信管理后台是管理多个微信帐号的平台,登录到平台的用户有三个角色,游客和微信帐号管理员.超级管理员.超级管理员负责建立新的微信帐号.建立新的微信帐号管理员:微信帐号管理员负责维护微信菜单:微信图文消息:处理微信事件,发布产品介绍专题等:游客的功能有浏览.下单.手机号绑定等.基于此我们分配了三个用户角色:ROLE_TRAVELER.ROLE_ADMIN.ROLE_SUPER分别对应游客.微信帐号

axios设置请求拦截和响应拦截

首先我们先创建axios实例 const service = axios.create({ baseURL: url, //是用于请求的服务器 URL timeout: 5000, // 请求超时时间 如果请求话费了超过 `timeout` 的时间,请求将被中断 headers: {'X-Custom-Header': 'foobar'} // 自定义请求头 }); 其他属性参考:https://www.kancloud.cn/yunye/axios/234845 接下来我们来添加拦截器 //

vue中axios 配置请求拦截功能 及请求方式如何封装

main.js 中: import axios from '................/axios' axios.js 中: //axios.js import Vue from 'vue' import axios from 'axios' Vue.prototype.$http = axios //http request 封装请求头拦截器 axios.interceptors.request.use(config => { // console.log("request&quo

vue配置请求拦截器和响应拦截器

首先确保我们已经设置的store.js进行值的存取,这时候我们需要配置请求和响应的拦截器设置 main.js import Vue from 'vue' import App from './App' import router from './router' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import axios from 'axios' // 引入store i

axios reponse请求拦截以及token过期跳转问题

前两天项目中遇到了token拦截,需要在请求的header头里放置token,需要用到response拦截,调试过程中遇到了拿不到token的问题 我用的axios实例 let token = store.state.token instance.interceptors.request.use(config => { // 在发送请求之前做些什么 //判断是否存在token,如果存在将每个页面header都添加token config.headers['Content-Type'] = 'ap

SpringMVC拦截器(实现登录验证拦截器)

本例实现登陆时的验证拦截,采用SpringMVC拦截器来实现 当用户点击到网站主页时要进行拦截,用户登录了才能进入网站主页,否则进入登陆页面 核心代码 首先是index.jsp,显示链接 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 St