How can I access the current fullPath inside of a axios plugin in Nuxt.js?

Viewed 1560

I am trying to get the current path through route.fullPath from within a Nuxt plugin for Axios. It is working to a degree, but it looks like because it is coming from the context, it is only ever setting the path when the page was initially loaded. How can I get the current route.fullPath (after any route changes) at the time of the Axios error to show?

export default function ({ $axios, store, route, redirect }) {
  $axios.onError((error) => {
    if (error.response.status === 401) {
      store.commit('misc/setRedirect', route.fullPath);

      redirect('/sign-in');
    }
  });
}
1 Answers

That seems like a bug, but as a workaround, you could get the current route through app.router.currentRoute of the plugin's app parameter:

// plugins/axios.js
export default function ({ $axios, app }) {
  $axios.onError(error => {
    console.log(error, app.router.currentRoute)
  })
}
Related