How to put class=“active” to first element in vuejs for loop

前端 未结 2 1993
猫巷女王i
猫巷女王i 2021-02-04 00:16

I\'m trying to learn vue.js. I\'m adding list of elements, I want to add class=\"active\" only to the first element in the for loop. following is my code:



        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 00:43

    The easiest solution is to check every element if index is equal to 0 and then setting the active class (or the class you needed). Here is the code of class definition:

    :class="{ 'active': index === 0 }"
    

    Here is working example of making Bootstrap Tabs:

    
    

    Also, if you have multiple classes you can mix them like this:

    :class="['tab-pane fade', (index === 0 ? 'active in' : 'something_else')]"
    

提交回复
热议问题