rstacruz / nprogress

For slim progress bars like on YouTube, Medium, etc
http://ricostacruz.com/nprogress
MIT License
26.15k stars 1.82k forks source link

use `vue-router` Maximum call stack size exceeded #221

Closed d1y closed 4 years ago

d1y commented 4 years ago
import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import Login from '_p/login/login.vue'
import Todo from '_p/todo/index.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/todo', component: Todo }
  ]
})

router.beforeEach((to, from, next)=>{
  if (to.path === '/login') return next()
  const tokenStr = window.localStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  } else {
    NProgress.start()
    return next('/home')
  }
})

router.afterEach(()=> {
  NProgress.done()
  NProgress.remove()
})

export default router
vue-router.esm.js?1bee:2117 RangeError: Maximum call stack size exceeded
    at abort (vue-router.esm.js?1bee:2116)
    at iterator (vue-router.esm.js?1bee:2181)
    at step (vue-router.esm.js?1bee:1884)
    at runQueue (vue-router.esm.js?1bee:1892)
    at HashHistory.confirmTransition (vue-router.esm.js?1bee:2185)
    at HashHistory.transitionTo (vue-router.esm.js?1bee:2072)
    at HashHistory.push (vue-router.esm.js?1bee:2497)
    at eval (vue-router.esm.js?1bee:2173)
    at eval (index.js?a18c:26)
    at iterator (vue-router.esm.js?1bee:2158)

what happened?

anselal commented 4 years ago

Usually this means you have an error in your vue-router config and it is unrelated to this project. Let me check..

anselal commented 4 years ago

return next('/home')

There is no such route

d1y commented 4 years ago

return next('/home')

There is no such route

BT. I found that it was indeed a problem with my routing configuration, which caused an infinite loop.

anselal commented 4 years ago

@d1y i new this cause i work with vuejs too, and i faced this error too sometimes