vue-router中的鉤子函數

  1. 全局路由鉤子函數
    每次路由跳轉,都會執行beforeEach和afterEach,一般寫在main.js可以做權限控制
    例如:
    router.beforeEach((to, from, next) => {
      if (to.matched.length === 0) {
        from.name ? next({ name : from.name }) : next('/')
      } else {
        next()
      }
    })
    router.afterEach((to,from) => {
      console.log(to);//到達的路由
      console.log(from);//離開的路由
    })

     

  2. 單個路由鉤子函數
    beforeEnter有三個參數:to/from/next
  3. 組件內路由鉤子函數
    三個參數:to/from/next 
    beforeRouteEnter:進入這個組建路由之前 
    beforeRouteLeave:離開這個組建路由 
    beforeRouteUpdate:再本路由的下級路由切換纔會觸發beforeRouteUpdate
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章