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 import store from ‘./store‘ // 如何localStorage的token不存在或是空跳转到路由 router.beforeEach((to, from, next) => { if (to.path === ‘/‘) { next(); } else { let token = window.localStorage.token; if (token === ‘null‘ || token === ‘‘ || token === undefined) { next(‘/‘); } else { next(); } } }) //与后端定义状态是100签名错误 跳转到登录界面 axios.interceptors.response.use( response => { //当返回信息为未登录或者登录失效的时候重定向为登录页面 if (response.data.status == 100 || response.data.message == ‘用户未登录或登录超时,请登录!‘) { router.push({ path: "/", querry: { redirect: router.currentRoute.fullPath }//从哪个页面跳转 }) } return response; }, error => { return Promise.reject(error) } )
文章来自:https://www.cnblogs.com/Jack-cx/p/12081745.html