【问题标题】:VueJS Axios Post Request - Can't update data bindingVueJS Axios 发布请求 - 无法更新数据绑定
【发布时间】:2019-08-03 13:47:52
【问题描述】:

我正在尝试使用以下方法更新我的 VueJS 页面。为了演示它在哪里工作以及在哪里不工作,我在下面添加了解释,但我被困住了如何绑定数据。

<script>
      window.app = new Vue({
        el: '#app',
        data: {
          form: {
            email: '',
            password: ''
          },
          token: ''
        },
        methods: {
          onSubmit(e) {
                  e.preventDefault();
                  this.token = "This works!"
                  // alert(JSON.stringify(this.form))
                  axios.post('http://localhost:5000/login', {
                      username: this.form.email,
                      password: this.form.password
                  })
                  .then(function (response) {
                      // this.token = response.data.access_token
                      this.token = "This does not work!"
                      console.log(response);
                  })
                  .catch(function (error) {
                      console.log(error);
                  });
          },
          onReset(e) {
            e.preventDefault()
            // Reset our form values
            this.form.email = ''
            this.form.password = ''
          }
        }
      })
    </script>

【问题讨论】:

  • 你的数据对象在哪里?
  • 刚刚更新了完整的脚本标签

标签: vue.js vuejs2


【解决方案1】:

this.token 内部函数不是 Vuejs 实例。你应该为它使用箭头功能

onSubmit(e) {
    e.preventDefault();
    this.token = "This works!"
    axios.post('http://mydomain/login', {
        username: this.form.email,
        password: this.form.password
    })
    .then((response) => {
        this.token = "This does not work!"
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });
}

【讨论】:

  • 感谢伊图斯!那行得通,一会儿就会接受。我是 JavaScript 中 => 的新手..
猜你喜欢
  • 2021-08-11
  • 2020-05-12
  • 1970-01-01
  • 2022-01-28
  • 2020-06-21
  • 2019-11-14
  • 2021-11-19
  • 2022-12-10
  • 1970-01-01
相关资源
最近更新 更多