[Vuejs]-Nuxt vue-router beforeRouteLeave navigation guard cause maximum call stack size exceeded when I check if to.hash is empty

3👍

I got an answer that helped me in Vue forum, I didn’t know that next() was recalling the process and that my beforeRouteLeave hook was reevaluating the new to.hash I setted causing the loop (to.hash !== '' was always true).

Be sure to test a value to the opposite that will pass the condition after you set it the 1rst time.

here’s the code to fix the problem:

  beforeRouteLeave (to, from, next) {
    if (to.name.startsWith('index___') && (to.hash === '#ct' | (to.hash !== `#${this.$route.params.id}` && to.hash !== ''))) {
      next({ path: to.path, hash: `#${this.$route.params.id}` })
    } else {
      next()
    }
  }

I hope it can help someone else.

👤Jecko

Leave a comment