Vue v-for object does not update after data changed

前端 未结 1 728
梦谈多话
梦谈多话 2021-01-24 17:28

I use vue version 2.5.x

Example like below.

No matter how hard I click \"add item\" the button, no item show in page.

But the \"add item version 2\" wo

1条回答
  •  温柔的废话
    2021-01-24 18:03

    Replace : this.items[this.count.toString()] = this.count;

    with Vue.set(this.items,this.count.toString(),this.count);

    let app = new Vue({
      el: "#app",
      data: {
        items: {},
        count: 0,
      },
      methods: {
        add() {
            Vue.set(this.items,this.count.toString(),this.count);
    
          this.count++;
          console.log(this.items);
        },
        add2() {
          let items = {}
          items[this.count.toString()] = this.count;
          this.items = items;
          this.count++;
          console.log(this.items);
        }
      }
    })
    

    https://jsfiddle.net/zbb57ozv/

    It's a limitations of javascript or Vue js caveat . check out section Change Detection Caveats of vue document for more information : Vue-Js Reactivity In Depth

    0 讨论(0)
提交回复
热议问题