Vue how to use vuerouter to jump in the newly created axios.js interceptor file

the newly created http.js sets the interceptor. How to determine if you are not logged in in the request interceptor, and then use vue-router to jump to the login page

import axios from "axios"
import { Message } from "element-ui"
import Router from "vue-router"

axios.interceptors.request.use(function (config) {
    // 
    console.log("")
    Router.push("/login")//
    return config;
}, function (error) {
    // 
    Message.error({message: "", duration: 2000})
    return Promise.reject(error);
});

report an error like this

Mar.23,2021

create a new routing instance in the routing file router.js

//router.js
const router = new vueRouter({
    routes,
    mode: 'history',
    base: __dirname,
    ...
});
...
export default router;

introduce router where routing is needed

//axios.js
import router from 'router.js';
router.push(link);

it doesn't work here. Use window.location.href to jump


main.js file
const rootVueObj = new Vue ({

router: router,
render: h => h(App),

}). $mount ('- sharpapp');
export default rootVueObj;

api file
import rootVueObj from'. / main.js';
use rootVueObj instead of this in interceptor


Axios is not suitable for unlogged-in redirects
if the best way to judge unlogged-in redirects is

import router from './router'

router.beforeEach((to, from, next) => {
  if (getToken()) { // token
      // 
    } else {
      next('/login') // 
    }
  }
})

axios.interceptors.response.use (
response = > {

)
 if(response.data.code == "-999"){
    layer.msg(",")
     localStorage.clear();
     store.dispatch("add2")
     store.dispatch("del1")
     router.push({path: "/login"})
 }
return response;

},
error = > {

  return Promise.reject(error)

});

Menu