一、全局守卫(进入任何一个路由都会执行)

beforeEach:进入路由前执行

beforeResolve:

afterEach(这个的回调参数没有next):导航完成时执行

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforEach((to,from,next)=>{
    ...
    //to是将要进去的路由,from是正要离开的路由
    //next()用来resolve这个钩子,next(false)不让它跳转,next('/home'),next({path:'/home'})
})

二、路由守卫(进入某个路由才会执行)

beforeEnter:进入该路由之前

[
    {
        path:'/home',
        component: Home,
        beforeEnter:(to,from,next)=>{...}
    }
]

三、组价守卫(进入某个组件才会执行)

beforeRouteEnter:进入组件时

beforeRouteUpdate:路由不变,传递的参数改变

beforeRouteLeave: 离开组件前

{
    data(){return {}},
    beforeRouteEnter(to,from,next){
        ...
        next()
    }
}

 

Logo

前往低代码交流专区

更多推荐