【问题标题】:Vue router allways redirecting to error pageVue路由器总是重定向到错误页面
【发布时间】:2020-11-13 06:36:15
【问题描述】:

我正在尝试在用户未登录时设置重定向。但是当我像在我的示例中那样执行此操作时,URL 会更改,但我会从 nuxt 获得 This page could not be found。该代码位于 plugins 文件夹内的 login.js 中。然后我像这样将它包含在 nuxt 配置中。

plugins: [
    '~/plugins/login.js'
],

这是处理重定向的实际代码

export default ({ app, store }) => {
  app.router.beforeEach((to, from, next) => {
    const loggedIn = store.state.account.loggedInAccount
    if (!loggedIn) {
      if (to.path !== '/redirect') {
        next({ path: '/redirect' })
      } else {
        next()
      }
    } else {
      next()
    }
  })
}

看起来路由还没有挂载。

【问题讨论】:

    标签: vue.js nuxt.js vue-router


    【解决方案1】:

    您应该尝试使用middleware。这是官方文档中提到的实现beforeEach函数的常规方式。您可以从here 了解它。如果可以访问中间件内部的route对象、store对象和redirect函数,那么验证后使用redirect指向其他路由。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 2020-05-26
      相关资源
      最近更新 更多