vue 统一处理token失效问题

使用http response 拦截器 在main.js中添加

import axios from 'axios';

  

axios.interceptors.response.use(response => {
  if (response) {
    switch (response.data.code) {
      case 403: //与后台约定登录失效的返回码,根据实际情况处理
        localStorage.clear();     //删除用户信息
        //如果超时就处理 ,指定要跳转的页面(比如登陆页)
        Message({
          message: 'token失效,请重新登录!',
          type: 'warning',
          center: true,
          onClose:function () {
            console.log('关闭了看下')
            router.replace({
              path: '/user/login',
            })
          }
        });
    }
  }
  return response;
}, error => {
  return Promise.reject(error.response.data) //返回接口返回的错误信息
})

  

原文地址:https://www.cnblogs.com/wupeng88/p/10636564.html