Vue loop input list, and solve the strange phenomenon of v-model after manually adding input

There are two ways to write the add method of

methods. The result is that the first writing method of v-model is normal, and the second is not correct. The reason for solving the problem is

.

effect of the first way of writing:

:

<template>
  <div style="width:100%;overflow:hidden;">
    <ul class="list">
      <li v-for="(item, index) in arrs">
        <input
          type="number"
          v-model="item.customItem"
          @input="changeFunc(item, index)"
        />
      </li>
    </ul>
    <button @click="newadd"></button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      arrs: [{
        "value": 1,
        "customItem": 1
      },{
        "value": 2,
        "customItem": 2
      }],
      na: {
        value: "",
        customItem: ""
      }
    }
  },
  methods: {
      newadd() {
      newadd() {
        // 
        // this.arrs.push({
        //   value: "",
        //   customItem: ""
        // });
        //  v-model 
        this.arrs.push(this.na);
        console.log(this.na)        
      },
      changeFunc(item, index) {
        this.arrs[index].customItem = item.customItem;
        this.watchVal();
      },
      // 
      watchVal() {
        const arrs = this.arrs;
        if (arrs.length > 0) {
          for (let i = 0; i < arrs.length; iPP) {
            let customItem = arrs[i].customItem;
            if (customItem * 1 < 0) {
              this.$set(this.arrs[i], "customItem", 0);
            }
          }
        }
      }
  }
}
</script>
Jan.25,2022

because you are adding the same object


because the object corresponds to an index, which means that different indexes point to a unified actual object, it will change at the same time, and the correct result is that different indexes correspond to different objects


the problem lies here

clipboard.png

every time you push into the arrs array, it is the na object, that is, every time you click on the new, the arrs will look like this

arrs = [
    // 
    {
        'value': 1,
        'customItem': 1
    },
    {
        'value': 2,
        'customItem': 2
    },
    // push
    na,
    na,
]

every time you push is a na object, so the following v-model points to the na object.

if you use the following code here:

this.arrs.push(JSON.parse(JSON.stringify(this.na)));

this is just a solution, and of course there are others. This is the stupidest way for me. I have to figure it out on my own.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35df5-2bfaf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35df5-2bfaf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?