【问题标题】:Does Vue.JS work with AJAX http calls?Vue.JS 是否适用于 AJAX http 调用?
【发布时间】:2017-04-14 09:48:11
【问题描述】:

我正在尝试从我的 HTML 中执行以下操作:

var vm = new Vue({
      el: '#loginContent',
      data: {
        main_message: 'Login',
        isLoggedIn: false,
        loginError: '',
        loginButton:'Login'
      },
      methods: {
        onLogin: function() {
          //this.$set(loginSubmit, 'Logging In...');
          var data = {
            email: $('#email').val(),
            password: $('#password').val(),
          };
          $.ajax({
            url: '/api/login',
            data: data,
            method: 'POST'
          }).then(function (response) {
            if(response.error) {
              console.err("There was an error " + response.error);
              this.loginError = 'Error';
            } else {
              //$('#loginBlock').attr("hidden",true);
              console.log(response.user);
              if(response.user) {
                this.isLoggedIn = true;
              } else {
                this.loginError = 'User not found';
              }
            }
          }).catch(function (err) {
            console.error(err);
          });
        }
      }
    });

基本上用户按下登录按钮,onLogin 方法被调用,发送一个帖子到我的 API。该帖子运行良好,我确实在 .then() 承诺中得到了回复。

但是,尝试执行 this.isLoggedIn = true; 之类的操作并不会按照用户登录时我期望 HTML 执行的操作更新我的 DOM。

当我在 Promise 中得到响应并且找不到“vm”实例时,可能是我处于某种后台线程(抱歉,这里是移动开发人员)中?

谢谢

【问题讨论】:

    标签: ajax vue.js


    【解决方案1】:

    这可能是因为你的this 没有指向正确的范围,这个范围在$.ajax 调用中发生了变化,所以你只需要执行以下操作:

      methods: {
        onLogin: function() {
          //this.$set(loginSubmit, 'Logging In...');
          var data = {
            email: $('#email').val(),
            password: $('#password').val(),
          };
          var that = this
          $.ajax({
            url: '/api/login',
            data: data,
            method: 'POST'
          }).then(function (response) {
            if(response.error) {
              console.err("There was an error " + response.error);
              that.loginError = 'Error';
            } else {
              //$('#loginBlock').attr("hidden",true);
              console.log(response.user);
              if(response.user) {
                that.isLoggedIn = true;
              } else {
                that.loginError = 'User not found';
              }
            }
          }).catch(function (err) {
            console.error(err);
          });
        }
      }
    

    【讨论】:

    • 非常感谢您! var that = this 对我来说真的很关键!我不明白为什么一切都超出了范围!
    【解决方案2】:

    我会建议另一种使用 ES6 箭头函数的方法,例如“=>”。它很简单,不需要额外的变量。如下所示:

          $.ajax({
            url: '/api/login',
            data: data,
            method: 'POST'
          }).then((response) => {
            if(response.error) {
              console.err("There was an error " + response.error);
              this.loginError = 'Error';
            } else {
              //$('#loginBlock').attr("hidden",true);
              console.log(response.user);
              if(response.user) {
                this.isLoggedIn = true;
              } else {
                this.loginError = 'User not found';
              }
            }
          }).catch(function (err) {
            console.error(err);
          });
    

    【讨论】:

      【解决方案3】:

      您可能想看看axios。我使用 $.ajax 并让它工作,但找到 axios 并且更喜欢 axios 而不是 ajax 库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-17
        • 2011-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-30
        相关资源
        最近更新 更多