How to redirect user inside vue-resource http inte

2019-04-08 09:07发布

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 
        }

    });
  });

How can I make this?

Using: Vue-router and Vue-resource

1条回答
淡お忘
2楼-- · 2019-04-08 09:36
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(...) 
        }
    });
 });
查看更多
登录 后发表回答