简体   繁体   English

数据未在Vue中更新

[英]Data not updating in Vue

When I trigger the method, I get the string I wanted back but not sure why it is not saved on the data object. 当我触发该方法时,我得到了我想要的字符串,但不确定为什么它没有保存在数据对象上。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks 谢谢

new Vue({
  el: '#app',
  data: {
    output: '' // <- var test (string) 
  },
  methods: {
    searchProperty: function () {
        $.get(baseUrl, function(res) {
            var test = $(res).find('#label_bin_output').text();
            this.output = test;
        });
    }
  }
});

update: forgot to mention that I'm using jQuery for ajax request. 更新:忘了提到我正在使用jQuery进行ajax请求。

new Vue({
  el: '#app',
  data: {
    output: '' // <- var test (string) 
  },
  methods: {
    searchProperty: function () {
        var self = this;
        ^^^^^^^^^^^^^^^^
        $.get(baseUrl, function(res) {
            var test = $(res).find('#label_bin_output').text();
            self.output = test;
            ^^^^
        });
    }
  }
});

The problem is that "this.output" is in scope of Jquery; 问题是“this.output”在Jquery的范围内; (this == jquery). (这= = jquery)。

You need to make a reference to main obj: 你需要引用主obj:

new Vue({
   el: '#app',
   data: {
     output: '' // <- var test (string) 
   },
   methods: {
    searchProperty: function () {
        var _self = this; // ref to main...
        $.get(baseUrl, function(res) {
            // try console.log this and _self
            console.log(_self);
            console.log(this);
            var test = $(res).find('#label_bin_output').text();
            _self.output = test;
        });
    }
  }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM