Vue — 导航守卫

Vue 的导航守卫是 Vue Router 提供的一种机制,用于在导航过程中对路由进行控制和管理。通过导航守卫,你可以在路由导航前、导航后、以及路由更新前后等不同阶段执行特定的逻辑操作。

全局前置守卫 (Global Before Guards):

beforeEach(to, from, next):在路由跳转前执行,可以用来进行路由验证、权限控制等操作。

 

//to去哪个地址
//from 从哪儿来
//next是否放行 (1)直接放行到to的路径(2)next(/路径) 进行拦截到next的路径

//定义一个数组存放需要用户登录权限的页面
const authList = ['/pay','/myorder']
router.beforeEach((to,from,next)=>{

  if(!authList.includes(to.path)){
    next()
    return
  }

  const token = ''
  if(token!==''){
    next()
  }else{
    next('/my')
  }


})

 

posted on 2024-03-14 12:07  萬事順意  阅读(16)  评论(0编辑  收藏  举报