【问题标题】:"this is undefined" in vue function [duplicate]vue函数中的“这是未定义的”[重复]
【发布时间】:2020-10-08 00:46:36
【问题描述】:

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

我这样制作了拦截器,但我收到一条消息“这是未定义的”。

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(){}”,但同样的错误。

如何清除我的代码?

【问题讨论】:

  • 尝试设置 var _this = this,然后作为 _this 访问它。验证是否真的是导致问题的原因。 ()=>{} 应该可以正常工作
  • 只需从它们各自的文件中导入您的store 和router,例如import store form 'path/to/store 并使用store.dispatch()。与router 相同

标签: javascript vue.js vuejs2


【解决方案1】:

this 仅适用于 vue 组件,我怀疑您的代码不在任何组件中。然后你需要寻找如何访问 store 和 router 外部组件:access store outside of component vuejs

【讨论】:

  • 谢谢。这我必须更改存储和路由器参数。
猜你喜欢
  • 1970-01-01
  • 2020-05-06
  • 2013-04-12
  • 1970-01-01
  • 2021-11-04
  • 2020-11-06
  • 2021-07-15
  • 2020-07-13
  • 2023-03-21
相关资源
最近更新 更多