鍍金池/ 問答/HTML/ vue failed to convert exception to strin

vue failed to convert exception to string

使用了vue的導(dǎo)航守衛(wèi)之后就報錯

failed to convert exception to string
router.beforeEach(function(to, from, next){
    /* 路由發(fā)生變化修改頁面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;
回答
編輯回答
尛曖昧

你這樣寫會陷入無限循環(huán)的,給你個參考下

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'
  })
}
2017年5月6日 21:33