这样子写,删除时出现一个bug
删除其中某一个,然后再添加不会从头开始添加,会在后一位添加,怎么解决
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<style>
span{
display: inline-block;
margin-right: 6px;
}
.v-enter-from, .v-leave-to{
opacity: 0;
transform: translateY(30px);
}
.v-enter-active, .v-leave-active{
transition: all .5s ease-in;
}
.v-enter-to, .v-leave-from{
opacity: 1;
transform: translateY(0px);
}
.v-move{
transition: all .5s ease-in;
}
</style>
</head>
<body>
<div id="root"></div>
<script>
const app = Vue.createApp({
data(){
return {
list: [1,2,3]
}
},
template:`
<div>
<transition-group>
<span v-for="(item,index) in list" :key="item" @click="removeSelf(index)">{{item}}</span>
</transition-group>
<button @click="addItem">增加</button>
<button @click="removeItem">删除</button>
</div>
`,
methods:{
addItem(){
this.list.unshift(this.list.length+1);
},
removeItem(){
this.list.shift();
},
removeSelf(index){
this.list.splice(index,1);
}
}
})
app.mount('#root')
</script>
</body>
</html>14
收起
正在回答 回答被采纳积分+1
1回答



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星