What is the reason for $router' of undefined?

there is a requirement:
the backend return status code-999 indicates that the token expires and the front end needs to skip to the login page.

my code:
http.js

    import axios from "axios"
    import Vue from "vue"
    import VueRouter from "vue-router"
    import { Message } from "element-ui";
    import common from "./global.js"
    Vue.use(VueRouter)
    
axios.interceptors.response.use(
    response => {
        //token
        if(response.data.code == -999){
            Message({
                message: ",!",
                type: "warning"
            });
            this.$router.push("/login");
            this.common.delCookie("token");
            this.common.delLocal();
        }
        return response;
    },
    error => {
        Message({
            message: "",
            type: "warning"
        });
        return Promise.reject(error.response.data)
});

prompted "Cannot read property"$router" of undefined"

There is no $router attribute in

this


import router from '@/router';
...
router.push('/login');

who does your this point to


use the axios method to change the this point. Before using the axios method, the cache this pointer


js file does not point to new Vue (), but only in the .vue file points to Vue instance

.
Menu