【问题标题】:How to set beforeResolve navigation guard in Nuxt.js如何在 Nuxt.js 中设置 beforeResolve 导航守卫
【发布时间】:2018-11-15 15:16:36
【问题描述】:

有没有办法在 nuxt.config.js 中添加 beforeResolve 导航守卫?

我的 nuxt.config.js

module.exports {
    ...
    router: {
        beforeResolve(to, from, next) {
            if (this.$store.getters.isLoggedIn)
                 next('/resource')
        }
    }
    ...
}

但它永远不会被调用!

我一直在尝试根据用户在 vuex 商店的登录状态在安装组件之前实现重定向。

【问题讨论】:

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


【解决方案1】:

您有两种选择。您可以通过中间件或在相应页面中设置全局规则。

// middleware/route-guard.js
export default function ({ app }) {

    app.router.beforeResolve((to, from, next) => {
        if (app.store.getters.isLoggedIn) {
            next('/resource')
        } else {
            next();
        }
    });

}

// Nuxt Page Component
export default {
    beforeResolve (to, from, next) {
        if (this.$store.getters.isLoggedIn) {
            next('/resource')
        } else {
            next();
        }
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2020-08-07
    相关资源
    最近更新 更多