Vue.js - Add class to clicked button

后端 未结 1 568
借酒劲吻你
借酒劲吻你 2021-01-25 14:26

The following code is meant to display several products displayed by brand. when you press the brand\'s button it will display its products. this is working fine however I\'ve a

相关标签:
1条回答
  • 2021-01-25 14:58

    Try to add another data object property called currentIndex and update it to the clicked button index :

    // DATA
    data() {
        return {
             currentIndex:-1,
            isActive: false,
               ...
    

    inside the template bind the class as follows :class='{buttonActive : (index==currentIndex) }':

    <div class="buttonBrand">
      <button v-for="(element, index) in brand"   :key="index" :class='{buttonActive : (index==currentIndex)  }' @click="changeBrand(index)">
        <img v-bind:src="element.imageBrand" alt />
      </button>
    </div
    

    methods :

        changeBrand(index) {
            this.object = this.brand[index].products;
            this.currentIndex=index;
    
    
        }
    
    0 讨论(0)
提交回复
热议问题