Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

vue.js: failure to set multiple classes with a variable part

I need to add few different classes on a div: one is static, another one with a variable, the last one is depending on a inner prop. The next option fails: nothing’s in the output

<div v-for="card in cards" :key="card.id" class="card" :class="{`card${card.rank}`,'cardFade':isFadeProp}"> 
     <span class="card__number">{{ card.rank }} </span>       
</div>

Looks like a problem is in the second class with a variable inside a collection of classes.
Any elegant solutions?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think the correction lies in the way you’re concatenating the class names and using the object syntax for conditional classes. Maybe try the code below:

<div v-for="card in cards" :key="card.id" class="card" :class="[
  'card' + card.rank,
  { 'cardFade': isFadeProp }
]">
  <span class="card__number">{{ card.rank }}</span>       
</div>

‘card’ + card.rank correctly concatenates the string ‘card’ with the card.rank. I am not an expert on vue.js but you can as well try it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading