javascript - Vue js - 来自发布请求的数据将不会呈现

标签 javascript jquery html vue.js vuejs2

我有下一个 vue 实例:

 var immobles_destacats = ($('#immobles_destacats').length > 0) ? new Vue({
  el: '#immobles_destacats',
  data: {
    immobles: {}
  },
  methods: {
    add: function(i) {
      if (this.immobles[i].imatges.lenght == this.immobles[i].counter) {
        return this.immobles[i].counter = 0;
      }
      return this.immobles[i].counter++;
    },
    substract: function(i) {
      if (this.immobles[i].counter == 0) {
        return this.immobles[i].counter = this.immobles[i].imatges.lenght;
      }
      return this.immobles[i].counter--;
    }
  },
  mounted: function() {
    $.post('get_immobles_destacats', function(immobles, textStatus, xhr) {
      for (var i = 0; i < immobles.length; i++) {
        immobles_destacats.immobles[i] = immobles[i];
        immobles_destacats.immobles[i].counter = 0;
      }
    });
  }
}) : null;

以及下一个渲染数据“immobiles”的 html:

<div id="immobles_destacats">
    <div v-for="(immoble, key) in immobles" class="immoble"><img v-bind:src="immoble.imatges[immoble.counter].url"/>
      <input type="button" v-on:click="add(key)" value="+"/>
      <input type="button" v-on:click="substract(key)" value="-"/>
    </div>
  </div>

如您所见,我在已安装的函数中设置了数据“immobiles”,我得到了以下结果: google chrome Vue DevTools extension on the loaded page 当页面加载时,问题就出现了,没有任何渲染,是否是因为数据“immobiles”在填充post请求时没有触发“onchange-renderhtml”?如果是这样,我如何将此“onchange-renderhtml”设置为数据“immobiles”?

最佳答案

我幸运地解决了这个问题,我仍然不知道为什么这不起作用,但重点是现在它起作用了,这是新的 Vue 实例:

var immobles_destacats = ($('#immobles_destacats').length > 0) ? new Vue({
  el: '#immobles_destacats',
  data: {
    immobles: []
  },
  methods: {
    add: function(i) {
      alert(this.immobles[i].imatges.lenght)
      alert(this.immobles[i].counter)
      if (this.immobles[i].imatges.lenght == this.immobles[i].counter) {
        return this.immobles[i].counter = 0;
      }
      return this.immobles[i].counter++;
    },
    substract: function(i) {
      alert(this.immobles[i].counter)
      if (this.immobles[i].counter == 0) {
        return this.immobles[i].counter = this.immobles[i].imatges.lenght;
      }
      return this.immobles[i].counter--;
    }
  },
  mounted: function() {
    $.post('get_immobles_destacats', function(immobles, textStatus, xhr) {
      for (var i = 0; i < immobles.length; i++) {
        immobles[i].counter = 0;
        immobles_destacats.immobles.push(immobles[i]);
      }
    });
  }
}) : null;

请注意,现在我插入了该对象,并在插入它之前设置了计数器。 此外,数据“immobiles”现在是一个数组,而不是一个对象

关于javascript - Vue js - 来自发布请求的数据将不会呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45504065/

相关文章:

asp.net - asp.net 页面中的 css 粘性页脚

javascript - 如何仅在 Blogger 的搜索页面中显示文本?

javascript - 使用 webworker 的 chrome 多线程功能?

php - 图片上传失败

jquery - 在 RStudio 中 Shiny ,希望定制一个表

javascript - 下拉列表不触发javascript函数

html - 在 HTML5 或 svg 文件中缩放 SVG

javascript - Javascript 类中 "@"符号的用途是什么?

javascript - Ajax:将内容从另一个div加载到div中

javascript - X-可编辑:在 "click to edit"上停止传播