How to solve Vue Router infinite redirect error
P粉403821740
2023-08-25 16:12:12
<p>In router.beforeEach, I want to check if the sessionToken already exists in the storage and if not redirect to the login page to get it, but I get the following error: </p>
<pre class="brush:php;toolbar:false;">Navigation guard detected infinite redirect when jumping from "/" to "/login". To avoid stack overflow, abort navigation. If not fixed, this will cause problems in production environments. </pre>
<p>Code in my router.js: </p>
<pre class="lang-js prettyprint-override"><code>router.beforeEach((to, from, next) => {
if(ENV == 'development') {
let sessionStorage = storage.sessionStorageGet('_sessionToken')
if (sessionStorage === null) next({ name: 'Login' })
else next()
}
})
</code></pre>