<view class="li" v-for="(item, index) in list" :key="index" :class="{on: currentIndex === index}" @click="toggleClass(index)">
{{item.txt}}
</view>
export default {
data() {
return {
list: [
{ txt: '选项1' },
{ txt: '选项2' },
{ txt: '选项3' },
{ txt: '选项4' },
{ txt: '选项5' },
],
currentIndex: '',
}
},
methods: {
toggleClass(index) {
this.currentIndex = this.currentIndex === index ? -1 : index;
}
}