0

I'm struggling to apply a conditional Vue statement to change the CSS style based on the text value. I have tried following other tutorials with no luck as I'm a beginner with Vue.

If for example I want the class to be is-success when {{s.specialType}} == "food" AND is-danger when {{s.specialType}} == "drink" how would I do it.

I would appreciate any help. Thanks

<div v-for="s in spc.specials" class="column is-one-third">
    <div class="field">
        <span class="tag is-success is-capitalized">{{s.specialType}}</span>
    </div>
</div>

2 Answers 2

4

Can be achieved using v-bind:class

<span v-bind:class="{'is-success': s.specialType==='food', 'is-danger': s.specialType==='drink'}" class="tag is-capitalized">
  {{s.specialType}}
</span>

Ref: https://v2.vuejs.org/v2/guide/class-and-style.html

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

Comments

0

After a quick google search for conditional rendering, i came up with the following solution:

<div v-for="s in spc.specials" class="column is-one-third">
<div class="field">
  <span class="tag is-success is-capitalized" v-if="s.specialType === 'food'">
    {{s.specialType}}
  </span>
  <span class="tag is-danger is-capitalized" v-else-if="s.specialType === 'drink'">
    {{s.specialType}}
  </span>
</div>

Vue.js documentation

1 Comment

Why play with rendering when Vue already has directives designed specifically for conditional class addition and removal?

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.