如何在vue-resource http拦截器内重定向用户?

时间:2017-04-03 01:42:56

标签: vuejs2 vue-router vue-resource

我希望在拥有401时重定向用户,但我无法从此上下文访问router

  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

1 个答案:

答案 0 :(得分:6)

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(...) 
        }
    });
 });