如何在 Nuxt.js 中设置 beforeResolve 导航守卫 [英] How to set beforeResolve navigation guard in Nuxt.js
本文介绍了如何在 Nuxt.js 中设置 beforeResolve 导航守卫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 nuxt.config.js 中添加 beforeResolve 导航守卫?
Is there a way to add beforeResolve navigation guard in nuxt.config.js?
我的 nuxt.config.js
My nuxt.config.js
module.exports {
...
router: {
beforeResolve(to, from, next) {
if (this.$store.getters.isLoggedIn)
next('/resource')
}
}
...
}
但它永远不会被调用!
我一直在尝试根据用户在 vuex 商店上的登录状态在安装组件之前实现重定向.
I've been trying to achieve a redirection before the component is mounted based on the users logged in state on the vuex store.
推荐答案
为此您有 2 个选项.您可以通过中间件或在相应页面中设置全局规则.
You have 2 options for this. You can set a global rule via the middleware or in the respective page.
// 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();
}
}
}
这篇关于如何在 Nuxt.js 中设置 beforeResolve 导航守卫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文