Nuxt 3 中间件无限导航卫士

问题描述 投票:0回答:1

我创建了 auth.global.ts

从“/auth/login”转到“/34/create-issue”时,在导航防护中检测到可能无限重定向。中止以避免堆栈溢出。 您是否总是在导航守卫中返回新位置?这会导致这个错误。仅在重定向或中止时返回,这应该可以解决此问题。如果不修复,这可能会导致生产中断。

export default defineNuxtRouteMiddleware(async (to, from) => {
    let authStore = useAuthStore()
    let { isLoggedIn } = authStore
    const token = useCookie('token')

    if (to.name == 'id-create-issue' && from.name !== 'id-create-issue') {
        return navigateTo(to.path, { replace: true })
    }

    if (isLoggedIn && to.path === '/auth/login') {
        return navigateTo('/company')
     }

     if (!token.value && to.path !== '/auth/login') {
        return navigateTo('/auth/login', { replace: true })
     }

}
nuxt.js vuejs3 middleware nuxtjs3 nuxt-middleware
1个回答
0
投票

尝试使用

to.name
代替
from.name
,就像这样
to.name == 'id-create-issue' && from.name !== 'id-create-issue'
to.name !== 'id-create-issue'

文档中所示。

© www.soinside.com 2019 - 2024. All rights reserved.