[Vuejs]-How to stop code execution after routing to error page from vuex?

0👍

For general errors like expired session I would recommend handle it low at axios level, using interceptor. https://github.com/axios/axios#interceptors

Interceptors can be defined eg. in plugins. Adding Nuxt plugin as example (Vue without Nuxt will use little bit different plugin definition, but still it should be useful as inspiration) (Also window is access because snippet is from SPA application – no server side rendering)

export default ({ $axios, redirect }) => {
  $axios.onError((error) => {
    const code = parseInt(error.response && error.response.status)
    if (code === 401) {
      // don't use route path, because error is happending before transition is completed
      if (!window.localStorage.getItem('auth.redirect')) {
        window.localStorage.setItem('auth.redirect', window.location.pathname)
      }
      redirect('/login-page')
    }
  })
}

Leave a comment