【问题标题】:How to redirect user inside vue-resource http interceptor?如何在 vue-resource http拦截器中重定向用户?
【发布时间】:2017-08-27 18:36:49
【问题描述】:

当我有 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

【问题讨论】:

    标签: vuejs2 vue-router 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(...) 
            }
        });
     });
    

    【讨论】:

    • 这行得通!但是如果我将这两个文件放在单独的文件中,我需要将路由器作为参数传递,对吗?我只是不明白我不能制作Vue.$router 或类似的东西.. 但感谢您的回答!
    • @thur 是的,如果它们在不同的文件中,您将需要导入或公开它,如 window.router 或类似的东西。
    猜你喜欢
    • 2020-01-25
    • 2014-06-07
    • 2017-02-01
    • 2015-08-22
    • 2021-03-22
    • 2017-04-20
    • 2016-09-24
    • 2016-10-31
    • 2014-05-26
    相关资源
    最近更新 更多