Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue 3 - Binding multiple classes to one condition

Tags:

vue.js

vuejs3

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>
like image 791
Adam Starrh Avatar asked Mar 19 '26 22:03

Adam Starrh


2 Answers

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>
like image 161
Husam Ibrahim Avatar answered Mar 23 '26 08:03

Husam Ibrahim


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

   <div
      class="static"
      :class="{ active: isActive || isSelectable }"
    ></div>
like image 43
catmal Avatar answered Mar 23 '26 09:03

catmal