【问题标题】:Javascript get data through methodsJavascript通过方法获取数据
【发布时间】:2017-03-12 12:22:44
【问题描述】:

我一直试图让这些代码工作,但我找不到正确的解决方案。请有人告诉我为什么这样做(加载时加载数据)但不会自动显示新记录。

<script>
Vue.component('comments',{
  template: '#comment-vue-template',
  data:() => {
    return {
        comments: []
        }
    },
  created: function(comments) {
    this.$http.get('/comments')
    .then(response => {
    this.comments = response.body
    });
    setTimeout(1000);
  },
  methods: {
    getComments: function(comments) {
      this.$http.get('/comments')
      then(response => {
        this.comments = response.body
      })
    },
  },
});
new Vue({
  el:'#app',
});
</script>

下面的代码根本不起作用:-

<script>
Vue.component('comments',{
  template: '#comment-vue-template',
  data:() => {
    return {
        comments: []
        }
    },
  created: function(comments) {
    this.getComments();
  },
  methods: {
    getComments: function(comments) {
      this.$http.get('/comments')
      then(response => {
        this.comments = response.body
      });
      setTimeout(this.getComments,1000);
    },
  },
});
new Vue({
  el:'#app',
});
</script>

提前致谢

【问题讨论】:

  • 您的开发者控制台中是否有任何错误?在您的第二个示例中,您在 then 之前缺少一个 .
  • @craig_h 发现了我的错误。谢谢

标签: javascript laravel vue.js vuejs2


【解决方案1】:

发现我的错误

<script>
Vue.component('comments',{
  template: '#comment-vue-template',
  data:() => {
    return {
        comments: []
        }
    },
  created: function() {
    this.getComments();
  },
  methods: {
    getComments() {
      this.$http.get('/comments').then(response => {
        this.comments = response.body
      });
      setTimeout(this.getComments,1000);
    }
  }
});
new Vue({
  el:'#app',
});
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 2021-10-08
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2016-11-14
    相关资源
    最近更新 更多