0

I have someting like this array:

items : [
   {
      cssClass : 'item1'
      text : 'some text 1'
   }

   {
      cssClass : 'item2'
      text : 'some text 2'
   }
]

i want make li that will have this cssClass class name

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

i need this type output

<ul>
    <li class="item1">some text 1</li>
    <li class="item2">some text 2</li>
</ul>

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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> 

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.