Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add class v-if using a conditional

Tags:

vuejs3

I just want to add a class to a using a conditional. I'm trying this code:

    <template>
  <div class="recipe" v-if="recipe.featured == 'true'" :class="recipe.featured">
    <button class="delete-recipe">
      <img src="@/assets/delete-button.svg" alt="Eliminar" title="Eliminar" />
    </button>
    <h2 class=".recipe-title">{{ recipe.title }}</h2>
    <div class="recipe-image"><img :src="recipe.image" /></div>
    <div class="recipe-info">
      <div class="recipe-info-item">
        <span class="recipe-info-label">Servings:</span>
        <span class="recipe-info-value"> {{ recipe.servings }} </span>
      </div>

I want to add the class "recipe.featured" to the div when recipe.featured props is true. I have json where I have my dates.

The css of recipe.featured is easy. I just want to paint the background. But at this moment is not working.

Any help?

Thanks.

like image 222
Jose Agudo Avatar asked Oct 20 '25 04:10

Jose Agudo


1 Answers

You can pass class conditionally using a ternary operator vue.js documentation

Example :

<div :class="recipe.featured ? 'myFirstClass' : 'mySecondClass'">

If you want to pass a class anyway you can pass it to a different class

<div :class="recipe.featured ? 'myFirstClass' : 'mySecondClass'" class="myClass">

Note: if you don't want to pass a class just put a blank class like* :class="recipe.featured ? 'myFirstClass' : ''

like image 160
RenaudC5 Avatar answered Oct 22 '25 04:10

RenaudC5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!