我创建了 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 })
}
}
尝试使用
to.name
代替from.name
,就像这样to.name !== 'id-create-issue'
如文档中所示。