原博客地址:html
Vue導航守衛beforeRouteEnter,beforeRouteUpdate,beforeRouteLeave詳解vue
vue組件獨享守衛鉤子函數參數詳解(beforeRouteEnter、beforeRouteUpdate、beforeRouteLeave)函數
// 組件在加載時調用,此時組件實例尚未被加載 beforeRouteEnter(to, from, next) { console.log(this, 'beforeRouteEnter') // undefined console.log(to, 'to') console.log(from, 'form') console.log(next, 'next()') next(vm => { // 由於當鉤子執行前,組件實例還沒被建立 // vm 就是當前組件的實例至關於上面的 this,因此在 next 方法裏你就能夠把 vm 當 this 來用了。 console.log(vm) // 當前組件的實例 }) }, beforeRouteUpdate(to, from, next) { // 在當前路由改變,可是該組件被複用時調用 // 對於一個帶有動態參數的路徑 /good/:id,在 /good/1 和 /good/2 之間跳轉的時候, // 因爲會渲染一樣的good組件,所以組件實例會被複用。而這個鉤子就會在這個狀況下被調用。 // 能夠訪問組件實例 `this` console.log(this, 'beforeRouteUpdate') // 當前組件實例 console.log(to, 'to') console.log(from, 'from') console.log(next, 'next()') next() }, beforeRouteLeave(to, from, next) { // 導航離開該組件的對應路由時調用 // 能夠訪問組件實例 `this` console.log(this, 'beforeRouteLeave') // 當前組件實例 console.log(to, 'to') console.log(from, 'from') console.log(next, 'next()') next() },