vue中要实现当前元素添加类同级兄弟元素移除类的效果,可使用v-bind:class;
v-bind:class支持变量,当变量发生变化的时候,class将发生变化;
实现方法举例:
<template>
<div>
<ul>
<li v-for="(item,index) in liList" :key="index" :class="{active:index==current}" @click="addClass(index)">{{item.purpose}}</li>
</ul>
</div>
</template>
<script>
export default {
data(){
return {
liList:[
{purpose:'洗车'},
{purpose:'美容'},
{purpose:'养护'},
{purpose:'换件'}
],
current:1,//默认哪个有类名
}
},
methods:{
addClass(index){
this.current=index
}
}
}