How to redirect user inside vue-resource http interceptor?

心不动则不痛 提交于 2019-12-05 23:02: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:


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


来源:https://stackoverflow.com/questions/43175412/how-to-redirect-user-inside-vue-resource-http-interceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!