vue-router动态路由无限循环

发布时间 2023-08-14 17:43:46作者: 绝版龙宝宝
// isLogined 用来判断用户是否已登录
router.beforeEach((to, from, next) => {
  if(isLogined){
    next()
  }else{
    console.log('测试')
    next('login')
  }
})


  • next() 表示放行,直接进入to路由,不会再次调用router.beforeEach()
  • next(path:...to,replace:true) 拦截路由,当条件成立时就会无限次放行,进入无限循环,必须要设置出口
  • next(path:'/') 当前路由被中断,进入一个新的导航  、需要加判断、不然会进入无限循环
  • 可以理解为
router.beforeEach((to, from, next) => {
  router.beforeEach((to, from, next) => {
    router.beforeEach((to, from, next) => {
    router.beforeEach((to, from, next) => {
      router.beforeEach((to, from, next) => {
         。。。
})
})
})
})
})
//必须要设置出口

router.beforeEach((to, from, next) => {
   let flag=true
    if(flag){
     //条件成立,放行并进入页面
    next()
    }else{
      //重载路由
      next({path:...to,replace:true})
    } })