【问题标题】:Vue router: blank page after routing from axios interceptorVue路由器:从axios拦截器路由后的空白页面
【发布时间】:2019-10-24 15:53:32
【问题描述】:

在我的axios.config.js 文件中,我有这段代码:

    axios.interceptors.response.use(
    config => config,
    error => {
      if (error && error.message === "Network Error") {
        if (router.currentRoute.name !== "login") {
          router.push({
            name: "errorPage",
            query: { template: "NoConnection" }
          });
        }
      } else if (error && error.response && error.response.status === 401) {
        Vue.auth.logout().then(() => {
          router.push({
            name: "login"
          });
        });
      } else if (
        error &&
        error.config.hasOwnProperty("errorHandle") &&
        error.config.errorHandle === false
      ) {
        return Promise.reject(error);
      } else {
        _notifyError(error);
      }
      return Promise.reject(error);
    }
  );

当我得到 401 状态代码时,会调用正确的代码,并且路由更改为:http://localhost:8080/#/error_page?template=UnAuthorized,但页面为空白。如果我刷新,我会看到错误页面。

我的路线:

 {
      path: "/error_page",
      name: "errorPage",
      component: ErrorPage
 },

有什么想法吗?

【问题讨论】:

  • 不熟悉axios,但是错误回调不应该返回一个Promise吗?试试return Vue.auth.logout().then ...
  • 你能显示你的路由配置吗?

标签: vue.js axios vue-router


【解决方案1】:

尝试将您的 router.push 对象更改为

router.push({
  path: "errorPage",
  query: { template: "NoConnection" }
});

当指定path 时,您只能在router.push 对象中使用query,否则如果您使用name,则必须使用params

如果这也不起作用,请尝试

router.push({
  name: "errorPage",
  params: { template: "NoConnection" }
});

{
  path: "/error_page/:templateName",
  name: "errorPage",
  component: ErrorPage
},

然后在您的ErrorPage 中获取带有$router 关键字的templateName 参数。

【讨论】:

  • paramsquery 是两个不同的东西,我使用它的方式是正确的。我看到路线更改为正确的路线,但没有显示任何内容。
  • 查看Vue路由器文档,在程序化导航中query关键字与path一起使用,这可能会影响路由导航。你能展示你的主要组件(你在哪里添加了router-view)和ErrorPage 组件吗?
  • 这里只是一个例子,不代表唯一的方法,其他地方也没有说。无论如何,它在路由方面起作用,它路由到正确的位置
  • 是的,我只是对其进行了测试,它在我的项目中运行,我没有得到空白页。如果不是我不知道,这可能是您的 ErrorPage 组件内部的问题
  • 其实是重定向问题。只有当我从 okta 等外部应用程序重定向时才会发生这种情况
猜你喜欢
  • 2020-02-20
  • 2020-07-28
  • 2018-02-16
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
相关资源
最近更新 更多