0

我有这样的数据:

  data: () => ({
    input: {
      username: "",
      password: ""
    }
  }),

并发出这样的帖子请求:

loggin_in() {
  if (this.input.username != "" && this.input.password != "") {
    console.log(this.input.username);
    axios
      .post("http://localhost:5000/login", {
        email: this.input.username,
        password: this.input.password
      })
      .then(function(data) {
        console.log(data);
      })
      .catch(function(error) {
        console.log(this.input.username);
      });
  }
}

记录第一个this.input.username作品,但在那之后,我不知道如何访问数据。this似乎是未定义的。

Uncaught (in promise) TypeError: Cannot read property 'input' of undefined

我不知道为什么this这里的上下文发生了变化,也不知道如何访问我的数据。任何人都可以向我解释发生了什么并帮助我解决这个问题吗?

4

1 回答 1

2

在回调中,您必须绑定 this 或使用箭头函数:

  .then(function(data) {
         console.log(data);
      })

  .then((data) => {
    console.log(data);
    console.log(this);
  })
于 2020-07-15T17:32:30.847 回答