javascript - 无法从组件方法访问数据

标签 javascript vue.js vuejs2 vue-component

我尝试了 vue js 中的组件方法。我的代码是这样的。

const Thread = Vue.component('threadpage', function(resolve) {
  $.get('templates/thread.html').done(function(template) {
    resolve({
      template: template,
      data: function() {
        return {
          data: {
            title: "Data Table",
            count: this.GetData
          }
        };
      },
      methods: {
        GetData: function() {
          var data =  {
            username : "newshubid",
            data :  {
              page : 0,
              length : 10,
              schedule : "desc"
            }
          };
          var args = {"data" : JSON.stringify(data)};
          var params = $.param(args);

          var url = "http://example-url";

          var result;

          DoXhr(url, params, function(response){
            result = JSON.parse(response).data;
            console.log("load 1", result);
          });

          setTimeout(function () {
            console.log("load 2", result);
            return result;
          }, 1000);
        }
      },
      created: function(){
        this.GetData();
      }
    });
  });
});

但是,当我尝试在模板中使用 {{ data.count }} 时。没有显示我想要的结果。即使我尝试在 GetData 中返回结果。

我的问题是什么?以及如何从方法中访问数据?请帮助我,我是初学者。谢谢

最佳答案

查看我在下面添加的编辑代码和注释。
您尝试在 setTimeout 的函数中使用 return 返回结果,这不会帮助您从 GetData 返回值。
相反,您可以只在 ajax 请求的回调函数中设置值。

const Thread = Vue.component('threadpage', function(resolve) {
  $.get('templates/thread.html').done(function(template) {
    resolve({
      template: template,
      data: function() {
        return {
          data: {
            title: "Data Table",
            // NOTE just set an init value to count, it will be refreshed when the function in "created" invoked.
            count: /* this.GetData */ {}
          }
        };
      },
      methods: {
        GetData: function() {
          var data =  {
            username : "newshubid",
            data :  {
              page : 0,
              length : 10,
              schedule : "desc"
            }
          };
          var args = {"data" : JSON.stringify(data)};
          var params = $.param(args);

          var url = "http://example-url";

          var result;
          var vm = this;
          DoXhr(url, params, function(response){
            result = JSON.parse(response).data;
            // NOTE set data.count to responsed result in callback function directly.
            vm.data.count = result;
          });
          // NOTE I think you don't need code below anymore.
          // setTimeout(function () {
          //   console.log("load 2", result);
          //   return result;
          // }, 1000);
        }
      },
      created: function(){
        this.GetData();
      }
    });
  });
});

关于javascript - 无法从组件方法访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45340951/

相关文章:

javascript - componentDidUpdate 中的先前状态和当前状态相等

javascript - IFrame 在 Firefox 中工作但在 IE 中不工作

android - NativeScript - 不支持可点击范围

vue.js - 在div vuejs中添加HTML元素

vuejs2 - 如何以编程方式确定 Buefy 输入字段是否有错误?

javascript - 通过这种方式使用shortid.generate()设置 react 键唯一?

javascript - 将属性应用于多个对象

webpack - 如何使用 Vuetify 的 Stylus CSS 类作为混入?

vue.js - Cloudflare Workers - 带有 Vuejs 的 SPA

javascript - 子级和父级之间共享 v-model 值