3

From the docs, it shows that we can bind classes to conditions like so:

<div
  class="static"
  :class="{ active: isActive, 'text-danger': hasError }"
></div>

What if I want to bind two classes to the same condition, would this work? Would there be a shorter way?

<div
  class="static"
  :class="{ active: isActive,  active: isSelectable }"
></div>
1
  • :class="{ active: isActive || isSelectable }" you can also separate that out into a helper method if the complexity grows too much. Commented Feb 28, 2022 at 20:48

2 Answers 2

8

If I understand you correctly you want to bind multiple classes to the same condition. There's nothing preventing you from doing it like this if you wanted to ..

<div
  class="static"
  :class="{ 'class-1 class-2': condition }"
></div>
Sign up to request clarification or add additional context in comments.

2 Comments

What if I want to concat class name with variable name? Like this :class="{ fa-${item.icon}: 2 == 2 }"
@MohammadAyoubKhan then you can use the dynamic property syntax like so - :class="{ [`fa-${item.icon}`] : 2 == 2 }" Ternaries are also good here inside the [ ] declaration.
2

You can use || and && to combine conditions:

   <div
      class="static"
      :class="{ active: isActive || isSelectable }"
    ></div>

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.