[Vuejs]-Vuejs add css class to ul li list from array

2👍

To bind key dynamically you can do like this

 <li v-for="item in items" :class="{[item.cssClass] : true}">{{item.text}}</li>

Or

  <li v-for="item in items" :class="item.cssClass">{{item.text}}</li> 

3👍

Simply do

<ul>
  <li v-for="item in items" :class="item.cssClass">{{item.text}}</li> 
</ul>

Here is a fiddle https://jsfiddle.net/mananvaghasiya/1ycvwwrb/4/ feel free to inspect list items to see the class name.

1👍

You can use directive v-bind for this:

<ul>
  <li v-for="item in items" v-bind:class="item.className">{{item.text}}</li> 
</ul>

or in short:

<ul>
  <li v-for="item in items" :class="item.className">{{item.text}}</li> 
</ul>

Leave a comment