全局守卫
const router = new Router({
router:[
{
path:'',
name:'',
component:''
}
]
})
router.beforeEach((to,form,next){
if(to.path == '/'){
next()
}else{
if('判断条件'){
next() //如果条件满足则跳转
}else{
next({path:'/'}) //如果条件不满足则跳转到login页面
}
}
})
export default router
局部守卫
const router = new Router({
router:[
{
path:'',
component:'',
beforeEnter(to,from,next){
if("是否满足判断条件"){
next() //如果满足测跳转
}else{
next('/'); //被拦截
}
},
children:[
{
path:"",
component:""
}
]
}
]
})
export default router
组件守卫
export default{
data(){
return {
}
},
beforeRouteUpdate(to,from,next){
//仅路由参数发生变化时触发,比如 /page/vue /page/react
next() //必要
}
}
路由生命周期结束
router.afterEach((to,from)=>{})