P
P
postya2020-10-15 06:12:08
Vue.js
postya, 2020-10-15 06:12:08

How to get rid of "Maximum call stack size exceeded" error in vue router?

I am making an application on VUE JS
There is a router that checks local storage before each route, whether there is a token or not.
You need to do this:
if there is no token -> redirect to "/authorisation"
if there is a token, then do nothing and show the page content, the routing that the user clicked on

I'm currently getting this error:

RangeError: Maximum call stack size exceeded


How can this be fixed?

index.js (vue router):
import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/analytics",
    name: "Analytics",
    component: () => import("../views/analytics")
  },
  {
    path: "/authorization",
    name: "Authorization",
    component: () => import("../views/authorization")
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  if (localStorage.getItem("token")) {
    next();
  } else {
    next("/authorization");
  }
});

export default router;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-10-15
@postya

If you are already going to the authorization form, do not try to initiate this transition again - add to beforeEach in if. || to.name === 'Authorization'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question