Vue-router beforeEach 在使用 next() 时陷入无限循环

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

我遇到了无法使用 next('/login') 的问题。这是我的代码:

*/ 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
  }
})

它在我只使用 next() 时有效,但当我为 next() 函数提供路由时,它陷入了无限循环

console screenshot

如果您已经定向到 '/login',则必须阻止调用 next('/login')

例如:

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