创建一个组件时,数据类型是数组,在删除这个数组中的数据时,数组中的数据是对的,但页面渲染的数据却不对。

举例:(不一定复现)

<ul>
<li v-for="(item, index) in peoples" :key="index">{{item.name}}
<span @click="del(index)">删除</span>
</li>
</ul>

<script>
export default {
data() {
return {
peoples: [
{
name: 'tom',
id: '1'
},
{
name: 'kitty',
id: '2'
},
{
name: 'jack',
id: '3'
}
]
}
},
methods: {
del (index) {
this.peoples.splice(index, 1)
}
}
}
</script>

原因:设置v-for时:key用的是index。key的值不是唯一造成的。
解决::key="item.id"设置成唯一的值
原理:待补

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-27
  • 2021-09-18
  • 2021-04-02
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
相关资源
相似解决方案