vue-路由守卫

vue中路由导航守卫,也叫做路由钩子函数,可以对用户要跳转的路由做一次检查,符合条件的就放行,不符合的强制用户跳转至登陆页面。

路由的导航守卫分为三种:

第一种:全局路由导航守卫:全局前置路由守卫和全局后置路由守卫。
全局前置路由守卫 router.beforeEach(切换之前调用,to、from、next参数)
//全局前置守卫,初始化时执行、每次路由切换前执行
router.beforeEach((to,from,next) =>{
    console.log('beforeEach',to,from)
    if(to.meta.isAuth){//判断当前路由是否需要进行权限控制
        if(localStorage.getItem('school' === 'atguigu'){//权限控制的具体规则
            next() //放行
        }else{
            alert('暂无权限查看')
            // next({name:'guanyu'})
        }
    }else{
        next() //放行
    }
})
// 例如
router.beforeEach((to,from,next) =>{
    if(to.path === '/home/news' || to.path === '/home/message'){
        if(localStorage.getItem('school') === 'atguigu'){
            next()
        }else{
            alert('用户名不符,无权查看')
        }
    }else{
        next()
    }
})
export default router
全局后置路由守卫router.afterEach(切换之后调用,to、from,没有next参数)
// 全局后置守卫:初始化时执行,每次路由切换后执行
router.afterEach((to,from) => {
    console.log('afterEach',to,from)
    if(to.meta.title){
        document.title = to.meta.title //修改网页title
    }else{
        document.title = 'vue_test'
    }
})

// 例如:当我们更换组件之后,要求页面的title也会跟着修改
router.afterEach((to,from) => {
    document.title = to.meta.title || '学习系统'
})
第二种:组件内路由守卫(在组件内编写)
//进入守卫:通过路由规则,进入该组件时被调用
beforeRouteEnter(to,from,next){},
//离开守卫:通过路由规则,离开该组件时被调用
beforeRouteLeaver(to,from,next){},
// 通过路由规则,进入该组件时被调用
beforeRouteEnter(to,from,next){
    //判断是否需要鉴定一下权限
    if(to.meta.isAuth){
        if(localStorage.getItem('school') === 'atguigu'){
            next(vw=>{
                alert('Hello' + vm.name)
            })
        }else{
            alert('无权限')
        }
    }else{
        next()
    }
}
// 离开该组件时被调用
beforeROuteLeave(to,from,next){
    if(confirm("确定离开此页面吗?") == true){
        next();
     }else{
        next(false);
     }
}
第三种:路由独享守卫beforeEnter(某一个路由所单独享用的,只有前置没有后置)
// 对某一个路由做出限制
beforeEnter(to,from,next){
    console.log('beforeEnter',to,from)
    if(to.meta.isAuth){ // 判断当前路由是否需要进行权限控制
        if(localStorage.getItem('school') === 'atguigu'){
            next()
        }else{
            alert('暂无权限查看')
        }
    }else{
        next()
    }
}
{
    path: '/',
    name: 'home',
    component: HomeView,
    // 嵌套路由(子路由)
    children:[
      {
        path:'/home/one',
        component:one,
        // 路由单独钩子(router独享守卫)
        beforeEnter:(to,from,next)=>{
          console.log('进入前执行');
          next();
        }
      }
    ]
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue-router提供了导航守卫来保护路由的导航。导航守卫可以在跳转或取消导航的过程中进行拦截。导航守卫可以分为全局守卫、路由独享守卫和组件级守卫。 全局守卫是注册在router实例上的,可以使用router.beforeEach方法注册一个全局前置守卫。在每次路由切换之前,全局前置守卫会被触发并接收to、from和next三个参数。可以在全局前置守卫中进行一些权限验证或者其他操作,然后通过调用next方法来进行导航。 路由独享守卫是在定义路由的时候通过beforeEnter属性来注册。路由独享守卫只会对特定的路由生效,可以在路由配置中定义一个beforeEnter函数来进行拦截操作。 组件级守卫是使用vue-router提供的beforeRouteEnter、beforeRouteUpdate和beforeRouteLeave方法来注册。beforeRouteEnter在进入路由前被调用,而beforeRouteUpdate在当前路由复用时被调用,beforeRouteLeave在离开当前路由时被调用。这些守卫可以在组件内部进行定义,并可以在组件内部进行一些操作,比如获取数据或者进行一些清理工作。 总结起来,vue-router的路由守卫提供了全局守卫、路由独享守卫和组件级守卫来保护路由的导航,可以在不同的阶段进行各种操作,确保路由的安全和灵活性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue路由守卫(导航守卫)](https://blog.csdn.net/m0_48949881/article/details/122436528)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值