1材失、全局的鉤子
beforeEach(to蓝丙,from磨淌,next)
- 頁(yè)面加載之前
- 頁(yè)面跳轉(zhuǎn)之前權(quán)限認(rèn)證橙垢,訪問(wèn)攔截
afterEach(to,from伦糯,next)
- 頁(yè)面加載之后
2柜某、組建內(nèi)的導(dǎo)航鉤子
beforeRouteEnter、beforeRouteUpdate敛纲、beforeRouteLeave,直接在路由組件內(nèi)部進(jìn)行定義的
beforeRouteEnter(to, from, next)
- 在渲染該組件的對(duì)應(yīng)路由被 confirm 前調(diào)用,
- 因?yàn)楫?dāng)鉤子執(zhí)行前喂击,組件實(shí)例還沒(méi)被創(chuàng)建,不能獲取組件實(shí)例‘this’
beforeRouteUpdate (to, from, next)
- 在當(dāng)前路由改變,但是該組件被復(fù)用時(shí)調(diào)用淤翔,即原路由帶有動(dòng)態(tài)參數(shù)翰绊,例如:/detail/:id,id值改變,即路由改變旁壮,但原本組件被復(fù)用监嗜;
- 訪問(wèn)組件實(shí)例
this
beforeRouteLeave (to, from, next)
- 導(dǎo)航離開(kāi)該組件的對(duì)應(yīng)路由時(shí)調(diào)用
- 可以訪問(wèn)組件實(shí)例
this
- 用途:清除當(dāng)前組件中的定時(shí)器,避免占用內(nèi)存抡谐;當(dāng)頁(yè)面中有未關(guān)閉的窗口, 或未保存的內(nèi)容時(shí), 阻止頁(yè)面跳轉(zhuǎn)裁奇;保存相關(guān)內(nèi)容到Vuex中或Session中
3、路由內(nèi)的導(dǎo)航鉤子
const router = new VueRouter({
routes: [
{
path: '/detail',
component: detail,
beforeEnter: (to, from, next) => {
// ...
}
}
]