18

I used style binding in Vue in this way:

v-bind:style="{'width': + width + 'px', 'left': + x + 'px', 'top': y + 'px'}"

When I required to bind multiple conditional classes, I used below syntax but it won't work!

v-bind:class="{(position)?position:'', (direction)?direction:''}"

Is there other way to apply multiple classes? Single class(without {}) works.

Here is the fiddle:

https://jsfiddle.net/kunjsharma/o0kL7myt/

2
  • 1
    Try this: :class="{ position, direction }", or v-bind:class="[(position)?position:'', (direction)?direction:'']" ([] instead of {}) Commented Jan 7, 2020 at 11:10
  • 1
    When I google "vue class binding" I get this page from the official docs as the top result, which easily answers your question. Commented Jan 7, 2020 at 11:59

3 Answers 3

34

The class binding expression in your template is invalid JavaScript syntax.

Did you mean to bind an array like this:

:class="[position, direction]"

So if position is 'right' and direction is 'rtl' then the element will have the classes right and rtl applied to it.

Binding an object is usually used when you have static class names that you want to apply conditionally based on some condition. Looking at your code, it doesn't seem like this is what you want to do.

For example, if you want to conditionally apply the static classes pressed and active based on some conditions, you can do this:

:class="{ pressed: pressedElement === el, active: !hidden }"

If pressedElement === el is true then the element will get the pressed class applied to it, likewise for active (I just made up some arbitrary expressions).

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

Comments

18

There are 2 syntaxes you can use:

  • Object: :class="{ position, direction }"
  • Array: :class="[position ? 'position' : '', direction ? 'direction' : '']"

You can also make :class a string, but that's messy when you want to use conditionals for multiple classes. You could also do a computed property:

computed: {
  myClass: function() {
    return [position ? 'position' : '', direction ? 'direction' : ''].filter(c => !!c).join('');
  }
}

See Conditional Classes in Vue for more examples.

1 Comment

Thanks for answering, much appreciated.
9

The object based syntax for class definition is:

{
    className: isEnabled,
    anotherName: isOtherEnabled,
}

where the key (className) is the name of the class and the value (isEnabled) is whether it is enabled or not.

So in your case you might need { position: position, direction: direction }. You could even let javascript infer the key names for you { position, direction }.

If you instead want to set the class-name to be the value of the position and direction properties then you should instead use the array syntax: [position, direction]. You can also achieve this with the class syntax like so: { [position]: true, [direction]: true }

1 Comment

Thanks @James. Altering {} to [] makes the difference.

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.