Vue路由防卫可以分为三类:路由独享守卫、组件内守卫和全局守卫
- 路由独享守卫
写在routes路径配置中,如以下代码
{path:’/A’,component:A,
beforeEnter:(to,from,next)=>{ }}
- 组件内守卫
写在组件js配置中,如以下代码
Export default{
name:’’,beforeRouteEnter:(to,from,next)=>{},
beforeRouteLeave:(to,from,next)=>{next(false);}
}
- 全局守卫
一般写在main.js中,如以下代码
const router=new VueRouter({})
router.beforeEach((to,from,next)=>{
if(to.path==’/A’){next()}
})
Router.afterEach((to,from)=>{
Alert()
})