vue failed to convert exception to string

使用了vue的导航守卫之后就报错

failed to convert exception to string
router.beforeEach(function(to, from, next){
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
        document.title = to.meta.title
    }
    if(!localStorage.getItem('token')){
        // router.push({name:'login'})
        // router.push({name: 'login'});
        next({ path: '/user/login' });
    }
    next();
});
export default router;

回答:

你这样写会陷入无限循环的,给你个参考下

let login = localStorage.getItem('token')
let path = to.path
if (path === '/user/login') {
  next()
  return
}
if (login) {
  if (path === '/') {
    next({
      path: '/index'
    })
  } else {
    next()
  }
} else {
  next({
    path: '/user/login'
  })
}

暂无评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注