码迷,mamicode.com
首页 > 其他好文 > 详细

beforeEach钩子,next('/login') 跳转问题,无线循环导致Maximum call stack size exceeded问题

时间:2020-04-08 18:53:54      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:死循环   判断   color   ssi   ESS   session   call   from   问题   

vue项目中,在router.js中判断登录状态时使用

beforeEach导致无线死循环Maximum call stack size exceeded

代码如下:

routes.beforeEach((to, from, next) => {
  if (sessionStorage.getItem(‘token‘)) {
    next();
  } else {
    next(‘/login‘);
  }
});

貌似一看没问题,但是却陷入了死循环,最后导致栈溢出。

原因:没有排除当前地址,就是/login地址,导致了循环调用。

解决如下:

routes.beforeEach((to, from, next) => {
  if (sessionStorage.getItem(‘token‘)) {
    next();
  } else {
    //如果是登录页面路径,就直接next()
    if (to.path === ‘/login‘) {
      next();
    } else {
      next(‘/login‘);
    }
  }
});

判断如果是登录页就放行,这样就不会死循环了。

beforeEach钩子,next('/login') 跳转问题,无线循环导致Maximum call stack size exceeded问题

标签:死循环   判断   color   ssi   ESS   session   call   from   问题   

原文地址:https://www.cnblogs.com/ouyangxiaoyao/p/12661649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!