Vue.js 2.0 splice has a problem deleting the last element

splice deletes the last item in the array

<div class="content" :index="index">
    

:{{tableList[index].name}}

<button @click="del(index)"></button> <button @click="cancel"></button> </div>

error code:

del:function(id){
    this.tableList.splice(id,1)
    for(i=0;i<this.tableList.length;iPP){
        this.tableList[i].id = i+1
    }
    this.show=false
}

error array:

  tableList:[
                {id:1,name:"",timer:new Date()},
                {id:2,name:"",timer:new Date()},
                {id:3,name:"",timer:new Date()},
                {id:4,name:"",timer:new Date()},
                {id:5,name:"",timer:new Date()},
                {id:6,name:"",timer:new Date()},
                {id:7,name:"",timer:new Date()}
            ]

contentpname


Jun.28,2022

is this error reported when you click "OK"? if so, it is because you click OK to delete the data of the current row, and the display data in your pop-up box at this time is also synchronously updated. When looking for the name under the current index, you did not find this field, so you reported an error


there is something wrong with your del method. The id of the tableList array is of no use.


suggest using array.filter


try putting this.show=false in front of this.tableList.splice (id,1) to execute


when deleting, you only need to pass in the index of a loop, and then use splice to

.
<div v-for="(item, index) in tableList">
<button @click="del(item, index)"></button>
</div>

del(item, index){
    this.tableList.splice(index, 1)
}
Menu