Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue-router beforeEach stuck in infinite loop when using next()

I'm having this problem where i cant use next('/login'). This is my code:

*/ all imports that are needed 

Vue.use(Router);

const routes = [
{path: '/', component: Home},
{path: '/admin', component: Admin},
{path: '/login', component: Login}]

var route = new Router({
routes: routes,
mode: 'history' });

route.beforeEach((to, from , next) => {
console.log(to.fullPath)
next('/login');
});

new Vue({
  el: '#app',
  router: route,
  template: '<App/>',
  components: {
    App
  }
})

It works when i only use next() but when i give the next() function a route it's stuck within an infinite loop

console screenshot

like image 293
frank Walleway Avatar asked Aug 31 '17 08:08

frank Walleway


1 Answers

You have to prevent to call next('/login') if you are already directing to '/login'.

For example:

route.beforeEach((to, from , next) => {
  console.log(to.fullPath)
  if (to.path !== '/login') {
    next('/login');
  } else {
    next();
  }
});
like image 105
Crackers Avatar answered Feb 23 '23 23:02

Crackers