如何在 vue-resource http 拦截器中重定向用户? [英] How to redirect user inside vue-resource http interceptor?

查看:25
本文介绍了如何在 vue-resource http 拦截器中重定向用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在出现 401 时重定向用户,但我无法从此上下文访问 router:

I want to redirect the user when I have an 401, but I can't access the router from this context:

  Vue.http.interceptors.push(function(request, next) {
    // continue to next interceptor
    next(function(response) {
        if(response.status == 401){
            //redirect user here
            //tried: Vue.$router and Vue.$route 
        }

    });
  });

我该怎么做?

使用:Vue-router 和 Vue-resource

推荐答案

const router = new VueRouter({
  routes
})

Vue.http.interceptors.push(function(request, next) {
    // continue to next interceptor
    next(function(response) {
        if(response.status == 401){
            router.push(...) 
        }
    });
 });

这篇关于如何在 vue-resource http 拦截器中重定向用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆