關于vue-router的beforeEach無限循環的問題解決
來源:懂視網
責編:小采
時間:2020-11-27 22:30:34
關于vue-router的beforeEach無限循環的問題解決
關于vue-router的beforeEach無限循環的問題解決:最近在使用vue-router的beforeEach鉤子時候遇到了一個問題,就是在beforeEach()中設置好判斷條件后出現了無限循環的問題 代碼如下: router.beforeEach((to, from, next) => { if(isLogin){ next() }else{ console
導讀關于vue-router的beforeEach無限循環的問題解決:最近在使用vue-router的beforeEach鉤子時候遇到了一個問題,就是在beforeEach()中設置好判斷條件后出現了無限循環的問題 代碼如下: router.beforeEach((to, from, next) => { if(isLogin){ next() }else{ console
最近在使用vue-router的beforeEach鉤子時候遇到了一個問題,就是在beforeEach()中設置好判斷條件后出現了無限循環的問題
代碼如下:
router.beforeEach((to, from, next) => {
if(isLogin){
next()
}else{
console.log('測試')
next('login')
}
})
結果chrome的debug中看到:

這個問題我是這樣理解的:
router.beforeEach((to, from, next) => {
if(true){
next()
}else{
next('login')
}
})
next() 表示路由成功,直接進入to路由,不會再次調用router.beforeEach()
next('login') 表示路由攔截成功,重定向至login,會再次調用router.beforeEach()
也就是說beforeEach()必須調用next(),否則就會出現無限循環,next() 和 next('xxx') 是不一樣的,區別就是前者不會再次調用router.beforeEach(),后者會!!!
官網這樣寫的(主要是紅線標記的那句!):

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
關于vue-router的beforeEach無限循環的問題解決
關于vue-router的beforeEach無限循環的問題解決:最近在使用vue-router的beforeEach鉤子時候遇到了一個問題,就是在beforeEach()中設置好判斷條件后出現了無限循環的問題 代碼如下: router.beforeEach((to, from, next) => { if(isLogin){ next() }else{ console