3路由有哪几种导航钩子

中等
推荐答案

全局守卫

auto 复制代码
// 一般用于登录验证
router.beforeEach((to, from, next) => {
  // do someting
});
  • beforeResolve:这个钩子和 beforeEach 类似,也是路由跳转前触发,参数也是 to,from,next 三个
    afterEach:和 beforeEach 相反,它是在路由跳转完成之后触发,参数包括 to、from,==没有了 next==

  • 路由独享守卫

    • beforeEnter:和 beforeEach 完全相同,如果都设置则在 beforeEach 之后紧随执行,参数有 to、from、next。
auto 复制代码
cont router = new VueRouter({
  routes: [
    {
      path: '/file',
      component: File,
      beforeEnter: (to, from ,next) => {
      // do someting
      }
    }
  ]
});

局部守卫

  • 是指在组件内执行的钩子函数,类似于数组内的生命周期函数,相当于为配置路由的组件添加的生命周期钩子函数。钩子函数按执行顺序包括 beforeRouteEnter、beforeRouteUpdate(2.2 新增)、beforeRouteLeave 三个

    auto 复制代码
    routes:[
       {
          path:'/b',
          component:B,
          beforeEnter:(to,form,next)=>{
             .....
          }
       }
    ]
相关回答
表情
图片