導航鉤子
vue-router 提供的導航鉤子主要用來攔截導航,讓它完成跳轉或取消。有多種方式可以在路由導航發生時執行鉤子:全局的, 單個路由獨享的, 或者組件級的。
全局鉤子
const router = new VueRouter({ ... })router.beforeEach((to, from, next) => { // do something next();});router.afterEach((to, from, next) => { console.log(to.path);});每個鉤子方法接收三個參數:
方法的調用參數。
確保要調用 next方法,否則鉤子就不會被 resolved。
組件內的鉤子
let fromPath = '';export default{ beforeRouteEnter (to, from, next) { // 在渲染該組件的對應路由被 confirm 前調用 // 不!能!獲取組件實例 `this` // 因為當鉤子執行前,組件實例還沒被創建 fromPath = from.path; next(); },}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答