![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL4ADMxQ2NkZTNyYGOyEWM5ImNmRjNzImNjZjN1YmY5QzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
這是陷入死循環中了,我的原因是:
寫vue登入健全時,條件寫的不正确,導緻了死循環。
const login=()=>!!localStorage.getItem('token')
router.beforeEach((to,from,next)=>{
if(login()){
next()
}else{
next('/login')
}
})
由于直接進行判斷是否有token,如果沒有就跳轉到登入頁面,但是忘記考慮到跳轉到登入頁面後,登入頁面也沒有token,是以下次在進入的時候,仍然進入的是登入頁面,進而陷入死循環中。
解決方法:
在登入的時候進行判斷,是否是在登入頁面或者是注冊頁面,如果是登入頁面或者是注冊頁面,直接就可以進入頁面,如果是在其他頁面需要進行判斷,是否存在token,如果存在就進入,如果不存在,就進行注冊。
const login=()=>!!localStorage.getItem('token')
router.beforeEach((to,from,next)=>{
const nextPath=['/login','/register']
if(nextPath.includes(to.path)){
next()
}else{
if(login()){
next()
}else{
next('/login')
}
}
})