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

猜你喜欢

转载自blog.csdn.net/WDCCSDN/article/details/82664179
今日推荐