vue-router中的鉤子函數(shù)和執(zhí)行順序說明
一:全局導(dǎo)航鉤子函數(shù)
1、vue router.beforeEach(全局前置守衛(wèi))
beforeEach的鉤子函數(shù),它是一個全局的before 鉤子函數(shù), (before each)意思是在 每次每一個路由改變的時候都得執(zhí)行一遍。
它的三個參數(shù):
to
: (Route路由對象) 即將要進入的目標 路由對象 to對象下面的屬性: path params query hash fullPath matched name meta(在matched下,但是本例可以直接用)from
: (Route路由對象) 當前導(dǎo)航正要離開的路由next
: (Function函數(shù)) 一定要調(diào)用該方法來 resolve 這個鉤子。 調(diào)用方法:next(參數(shù)或者空) ***必須調(diào)用- next(無參數(shù)的時候): 進行管道中的下一個鉤子,如果走到最后一個鉤子函數(shù),那么 導(dǎo)航的狀態(tài)就是 confirmed (確認的)
- next('/') 或者 next({ path: '/' }): 跳轉(zhuǎn)到一個不同的地址。當前的導(dǎo)航被中斷,然后進行一個新的導(dǎo)航。
- 應(yīng)用場景:可進行一些頁面跳轉(zhuǎn)前處理,例如判斷需要登錄的頁面進行攔截,做登錄跳轉(zhuǎn)!!
router.beforeEach((to, from, next) => { ? ? if (to.meta.requireAuth) { ? ? ? ? //判斷該路由是否需要登錄權(quán)限 ? ? ? ? if (cookies('token')) { ? ? ? ? ? ? //通過封裝好的cookies讀取token,如果存在,name接下一步如果不存在,那跳轉(zhuǎn)回登錄頁 ? ? ? ? ? ? next()//不要在next里面加"path:/",會陷入死循環(huán) ? ? ? ? } ? ? ? ? else { ? ? ? ? ? ? next({ ? ? ? ? ? ? ? ? path: '/login', ? ? ? ? ? ? ? ? query: {redirect: to.fullPath}//將跳轉(zhuǎn)的路由path作為參數(shù),登錄成功后跳轉(zhuǎn)到該路由 ? ? ? ? ? ? }) ? ? ? ? } ? ? } ? ? else { ? ? ? ? next() ? ? } })
應(yīng)用場景,進入頁面登錄判斷、管理員權(quán)限判斷、瀏覽器判斷
//使用鉤子函數(shù)對路由進行權(quán)限跳轉(zhuǎn) router.beforeEach((to, from, next) => { ? ? const role = localStorage.getItem('ms_username'); ? ? if(!role && to.path !== '/login'){ ? ? ? ? next('/login'); ? ? }else if(to.meta.permission){ ? ? ? ? // 如果是管理員權(quán)限則可進入,這里只是簡單的模擬管理員權(quán)限而已 ? ? ? ? role === 'admin' ? next() : next('/403'); ? ? }else{ ? ? ? ? // 簡單的判斷IE10及以下不進入富文本編輯器,該組件不兼容 ? ? ? ? if(navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor'){ ? ? ? ? ? ? Vue.prototype.$alert('vue-quill-editor組件不兼容IE10及以下瀏覽器,請使用更高版本的瀏覽器查看', '瀏覽器不兼容通知', { ? ? ? ? ? ? ? ? confirmButtonText: '確定' ? ? ? ? ? ? }); ? ? ? ? }else{ ? ? ? ? ? ? next(); ? ? ? ? } ? ? } })
2、vue router.afterEach(全局后置守衛(wèi))
router.beforeEach 是頁面加載之前,相反router.afterEach是頁面加載之后
二:路由獨享的守衛(wèi)(路由內(nèi)鉤子)
你可以在路由配置上直接定義 beforeEnter 守衛(wèi):
const router = new VueRouter({ ? routes: [ ? ? { ? ? ? path: '/foo', ? ? ? component: Foo, ? ? ? beforeEnter: (to, from, next) => { ? ? ? ? // ... ? ? ? } ? ? } ? ]
這些守衛(wèi)與全局前置守衛(wèi)的方法參數(shù)是一樣的。
三:組件內(nèi)的守衛(wèi)(組件內(nèi)鉤子)
1、beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave
const Foo = { ? template: `...`, ? beforeRouteEnter (to, from, next) { ? ? // 在渲染該組件的對應(yīng)路由被 confirm 前調(diào)用 ? ? // 不!能!獲取組件實例 `this` ? ? // 因為當鉤子執(zhí)行前,組件實例還沒被創(chuàng)建 ? }, ? beforeRouteUpdate (to, from, next) { ? ? // 在當前路由改變,但是該組件被復(fù)用時調(diào)用 ? ? // 舉例來說,對于一個帶有動態(tài)參數(shù)的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉(zhuǎn)的時候, ? ? // 由于會渲染同樣的 Foo 組件,因此組件實例會被復(fù)用。而這個鉤子就會在這個情況下被調(diào)用。 ? ? // 可以訪問組件實例 `this` ? }, ? beforeRouteLeave (to, from, next) { ? ? // 導(dǎo)航離開該組件的對應(yīng)路由時調(diào)用 ? ? // 可以訪問組件實例 `this` ? }
2、路由鉤子在實際開發(fā)中的應(yīng)用場景
(一) 清除當前組件中的定時器
當一個組件中有一個定時器時, 在路由進行切換的時候, 可使用beforeRouteLeave將定時器進行清楚, 以免占用內(nèi)存:
beforeRouteLeave (to, from, next) { ? window.clearInterval(this.timer) //清楚定時器 ? next() }
(二) 當頁面中有未關(guān)閉的窗口, 或未保存的內(nèi)容時, 阻止頁面跳轉(zhuǎn)
如果頁面內(nèi)有重要的信息需要用戶保存后才能進行跳轉(zhuǎn), 或者有彈出框的情況. 應(yīng)該阻止用戶跳轉(zhuǎn),結(jié)合vuex狀態(tài)管理(dialogVisibility是否有保存)
?beforeRouteLeave (to, from, next) { ?//判斷是否彈出框的狀態(tài)和保存信息與否 ?if (this.dialogVisibility === true) { ? ? this.dialogVisibility = false //關(guān)閉彈出框 ? ? next(false) //回到當前頁面, 阻止頁面跳轉(zhuǎn) ? }else if(this.saveMessage === false) { ? ? alert('請保存信息后退出!') //彈出警告 ? ? next(false) //回到當前頁面, 阻止頁面跳轉(zhuǎn) ? }else { ? ? next() //否則允許跳轉(zhuǎn) ? }
(三) 保存相關(guān)內(nèi)容到Vuex中或Session中
當用戶需要關(guān)閉頁面時, 可以將公用的信息保存到session或Vuex中
?beforeRouteLeave (to, from, next) { ? ? localStorage.setItem(name, content); //保存到localStorage中 ? ? next() }
vue-router執(zhí)行順序
- 導(dǎo)航被觸發(fā)。
- 在失活的組件里調(diào)用 beforeRouteLeave 守衛(wèi)。
- 調(diào)用全局的 beforeEach 守衛(wèi)。
- 在重用的組件里調(diào)用 beforeRouteUpdate 守衛(wèi) (2.2+)。
- 在路由配置里調(diào)用 beforeEnter。
- 解析異步路由組件。
- 在被激活的組件里調(diào)用 beforeRouteEnter。
- 調(diào)用全局的 beforeResolve 守衛(wèi) (2.5+)。
- 導(dǎo)航被確認。
- 調(diào)用全局的 afterEach 鉤子。
- 觸發(fā) DOM 更新。
- 調(diào)用 beforeRouteEnter 守衛(wèi)中傳給 next 的回調(diào)函數(shù),創(chuàng)建好的組件實例會
- 作為回調(diào)函數(shù)的參數(shù)傳入。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+webpack模擬后臺數(shù)據(jù)的示例代碼
這篇文章主要介紹了vue+webpack模擬后臺數(shù)據(jù)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07Vue.js綁定HTML class數(shù)組語法錯誤的原因分析
Vue.js綁定HTML class數(shù)組語法錯誤有哪些原因?qū)е碌哪?,該如何解決呢?下面小編給大家分享Vue.js綁定HTML class數(shù)組語法錯誤的原因分析,感興趣的朋友一起看看吧2016-10-10vue create、vue webpack init創(chuàng)建vue項目產(chǎn)生的項目文件的區(qū)別
這篇文章主要介紹了vue create、vue webpack init創(chuàng)建vue項目產(chǎn)生的項目文件的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11