vue项目动态绑定class

描述:如下图,点击哪个tabbar就让哪个的样式改变

 

思路:
定义一个变量currentIndex用来记录点击的是哪个,然后当点击的index == currentIndex时,就给它绑定一个class active
实现:
<template>
    <div class="tabcontrol">
        <div class="tabcontrol-item" v-for="(item, index) in feature" 
        :key="index"
        :class="{active: index === currentIndex}"
        @click="itemClick(index)">
            <span>{{item}}</span>
        </div>
    </div>
</template>
<script>
export default {
    name: 'TabControl',
    props: {
        feature: {
            type: Array,
            default: []
        }
    },
    data(){
        return {
            currentIndex: 0 //记录当前点击的是哪一个就
        }
    },
    methods: {
        itemClick (index) {
            //改变currentIndex
            this.currentIndex = index
        }
    }
}
</script>
<style scoped>
.tabcontrol {
    display: flex;
    text-align: center;
    background: #fff;
}
.tabcontrol-item {
    flex: 1;
}
.active {
    color: var(--color-high-text);
}
.active span {
    border-bottom: 2px solid var(--color-high-text);
  }
</style>
不积跬步无以至千里
原文地址:https://www.cnblogs.com/lyt0207/p/12458481.html