[Vuejs]-Using redirect in Vue/Nuxt Plugin

0👍

You can use context variable.

Example:

plugins/axios.js

import axios from 'axios'

export default function (context, inject) {
  axios.interceptors.response.use(res => {
    console.log('Interceptor response: ', res.data);
    return res;
  }, err => {
    if (err.response.status === 401) {
      context.redirect('/');
      context.store.dispatch(AUTH_LOGOUT);
    }
  });

  inject('axios', axios)
}

Inject in $root & context

Leave a comment