【问题标题】:Cannot access data from component method无法从组件方法访问数据
【发布时间】:2018-01-02 14:27:38
【问题描述】:

我在 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 中返回结果。

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

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component


    【解决方案1】:

    查看我在下面添加的已编辑代码和 cmets。
    您尝试在来自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();
          }
        });
      });
    });
    

    【讨论】:

    • 我试过你的代码,但我的 {{ data.count }} 没有显示。是真的吗?使用 {{ data.count }} ?
    • 天啊,我的错误。我忘了计数:{}。现在它的工作。非常感谢你
    猜你喜欢
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    相关资源
    最近更新 更多