Vue功能中的“ this is undefined”

时间:2020-06-18 03:33:06

标签: javascript vue.js vuejs2

是的,有很多这样的帖子,但是我不知道发生了什么(在这里和Google中寻找很多帖子)。

我使拦截器是这样的,但是我收到一条消息“ This is undefined”。

axios.interceptors.response.use(undefined, function(err) {

    return new Promise(function(resolve, reject) {

        if (err.request.status === 401 && err.config && !err.config.__isRetryRequest) {

            console.log('Disconnected. Session expires')

            this.$store.dispatch('logout')
            this.$router.push({ name: "login" });

        }

        throw err;

    });

首先,我有“()=> {}”,并且我已更改为“ function(){}”,但存在相同的错误。

如何清除代码?

1 个答案:

答案 0 :(得分:2)

this仅适用于vue组件,我怀疑您所涉及的代码不在任何组件中。然后,您需要查找如何访问外部组件的存储和路由器:access store outside of component vuejs