關(guān)于vue-router的beforeEach無限循環(huán)的問題解決
最近在使用vue-router的beforeEach鉤子時候遇到了一個問題,就是在beforeEach()中設(shè)置好判斷條件后出現(xiàn)了無限循環(huán)的問題
代碼如下:
router.beforeEach((to, from, next) => { if(isLogin){ next() }else{ console.log('測試') next('login') } })
結(jié)果chrome的debug中看到:
這個問題我是這樣理解的:
router.beforeEach((to, from, next) => { if(true){ next() }else{ next('login') } })
- next() 表示路由成功,直接進入to路由,不會再次調(diào)用router.beforeEach()
- next('login') 表示路由攔截成功,重定向至login,會再次調(diào)用router.beforeEach()
也就是說beforeEach()必須調(diào)用next(),否則就會出現(xiàn)無限循環(huán),next() 和 next('xxx') 是不一樣的,區(qū)別就是前者不會再次調(diào)用router.beforeEach(),后者會!??!
官網(wǎng)這樣寫的(主要是紅線標記的那句!):
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource
本篇文章主要介紹了詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03