Vue.js - updated array item value doesn't update in page

后端 未结 3 1754
抹茶落季
抹茶落季 2020-12-07 00:52

\"test\" is an array of object in my vue data

var vue = new Vue({
  el: \'#content\',

  data: {
    test: [
      {
        array: [0, 0, 0, 0]
      },
            


        
相关标签:
3条回答
  • 2020-12-07 01:26

    From

    https://vuejs.org/v2/guide/reactivity.html

    var vm = new Vue({
      data: {
        items: ['a', 'b', 'c']
      }
    })
    
    //vm.items[1] = 'x' // is NOT reactive
    Vue.set(vm.items, indexOfItem, newValue)  //works fine
    

    worked for me

    0 讨论(0)
  • 2020-12-07 01:31

    This is due to the array change caveats.

    Do it like this instead

    var vue = new Vue({
      el: '#content',
    
      data: {
        test: [{
          array: [0, 0, 0, 0]
        }, {
          array: [0, 0, 0, 0]
        }],
        number: 0
      },
    
      methods: {
        setNumber: function() {
          this.number = 5;
          console.log(this.number);
        },
        setArray: function() {
          //this.test[0].array[0] = 9;
          this.$set(this.test[0].array, 0, 9);
          console.log(this.test[0].array[0]);
        }
      }
    });
    

    Here is thefiddle

    0 讨论(0)
  • 2020-12-07 01:46

    Instead of updating items inside the array, try this

     this.users = Object.assign({},newList);
    

    This will update the DOM.

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