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

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

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