vue前端http拦截(后端接口返回401前端返回登录页面)


// response interceptor(接收拦截器)
axios.interceptors.response.use(function (response) {
    return response;
}, error => {
    let response = error.response;
    const status = response.status;
    if (status === 401) {
        // 判断状态码是401 跳转到登录
        route.replace({ path: "/login" });
    }
    return {
        data: {
            data: '',
            error: "请重新登录"
        }
    };
});

这里原先使用main.js配置的

this.$router.replace({ path: "/login" });

这样是没有用的,需要引入route文件

import route from '../router/index'

这样就可以正常拦截掉请求结果如果出现401就返回登录页面。

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM