vue 導航鉤子

全局導航鉤子:javascript

const router = new Vuerouter({...})

router.beforeEach(to,from,next){
   console.log(to)//從哪一個路由來
   console.log(from)//去到哪一個路由

   next()//必需要寫才能進入當前路由
   next('/login')//進入指定路由
}

router.afterEach(router=>{
   ....
})//進入當前頁面路由後,能夠作一些事情

單個路由的鉤子java

new Vuerouter({
   routers:[
        {
          path:'/login',
          component:Foo,
          beforeEnter(to,from,next){
//同全局導航鉤子
             }
        }
      
      }
   ]
})

組件內鉤子:this

beforeRouteEnter (to, from, next) {
    // 在渲染該組件的對應路由被 confirm 前調用
    // 不!能!獲取組件實例 `this`
    // 由於當鉤子執行前,組件實例還沒被建立
  },
  beforeRouteUpdate (to, from, next) {
    // 在當前路由改變,可是該組件被複用時調用
    // 舉例來講,對於一個帶有動態參數的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉的時候,
    // 因爲會渲染一樣的 Foo 組件,所以組件實例會被複用。而這個鉤子就會在這個狀況下被調用。
    // 能夠訪問組件實例 `this`
  },
  beforeRouteLeave (to, from, next) {
    // 導航離開該組件的對應路由時調用
    // 能夠訪問組件實例 `this`
  }
相關文章
相關標籤/搜索