VueJS 和 axios - 如何访问 post 请求中的数据

VueJS and axios - how to access data in a post request

我有这样的数据:

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

并像这样发出 post 请求:

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 的上下文会在这里发生变化,也不知道如何访问我的数据。任何人都可以向我解释发生了什么事并帮助我解决这个问题吗?

在回调中你必须绑定它或使用箭头函数:

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

  .then((data) => {
    console.log(data);
    console.log(this);
  })