2

What is the correct way to add a css class decided by a condition in Vue:

<div class={{current == key ? ' is-active' : '' }}> Page title</div>

2 Answers 2

6
<div :class="{'is-active': current === key }">

You just use an object where each class you desire is the key, and the value is the comparator.

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

Comments

1

This is the most convenient way to me, you can add multiple classes using conditions

<div :class="[current === key ? 'is-active' : '', otherClassCondition ? 'other-class' : '']">

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.