console
Vue.component('parent',{
data(){
return {
arr:[{id:1,value:parseInt(Math.random()*100)}]
}
},
template:`
<div>
<ul>
<li v-for="(item,index) in arr" :key="index">
<child></child>
<button @click="del(index)">删除</button>
</li>
</ul>
<button @click='add'>添加</button>
</div>
`,
methods:{
add(){
this.arr.push({id:Math.random()*100})
},
del(index){
this.arr.splice(index,1)
}
}
})
Vue.component('child',{
data(){
return {
val: parseInt(Math.random()*100)
}
},
template:`
<p>{{val}}</p>
`
})
new Vue({
el: '#app'
})
<div id="app">
<h2>v-for错误使用key</h2>
<parent>
</parent>
</div>