2

I have this div in my Field.vue component template:

<div class="field"
    :class="{
      'has-bomb': field.hasBomb && field.isOpen,
      'is-open': field.isOpen,
      'is-marked': field.isMarked
    }"></div>

Now I have a computed that creates a String based on field.x and field.y like this:

computed: {
  cssClass () {
    return `x${this.field.x}-y${this.field.y}`
  }
}

How can I add that generated String as a CSS class to my div?

I tried

<div class="field"
    :class="{
      'has-bomb': field.hasBomb && field.isOpen,
      'is-open': field.isOpen,
      'is-marked': field.isMarked,
      cssClass
    }"></div>

and also

<div class="field"
    :class="{
      'has-bomb': field.hasBomb && field.isOpen,
      'is-open': field.isOpen,
      'is-marked': field.isMarked,
      cssClass: true
    }"></div>

but that just gives me

<div class="field cssClass"></div>

instead of what I need, e.g.:

<div class="field x3-y10"></div>
4
  • try <div class="field" :class="{ 'has-bomb': field.hasBomb && field.isOpen, 'is-open': field.isOpen, 'is-marked': field.isMarked, this.cssClass() }"></div> Commented Jan 26, 2018 at 20:26
  • There is no this in the template in Vue.js. Commented Jan 26, 2018 at 20:27
  • what if u drop the 'this' and just do cssClass()? Commented Jan 26, 2018 at 20:31
  • Error compiling template: - invalid expression: Unexpected token } in { 'has-bomb': field.hasBomb && field.isOpen, 'is-open': field.isOpen, 'is-marked': field.isMarked, cssClass() } changes to ´Unexpected token :` when I try cssClass(): true Commented Jan 26, 2018 at 20:33

1 Answer 1

10

For this approach, Vue provides so called the Array Syntax class binding, which allow using static or dynamic classes inside an array which is passed to the dynamic html attribute.

For example:

<div :class="['static-class', computedClass, {'dynamic': obj.enabled}]">Test</div>

**REQUIRED IN COMMENTS by @webnoob:** It is also good to know that is possible to use both pure html `class` with vue dynamic `:class` even though in HTML is not valid to use 2 same attributes ([read more in specifications][2]), moreover, nowadays if you try to use multiple same attributes, browsers will just ignore them. But in our case with vue it works because all the dynamic classes will be merged together with the static ones and at the end the element will contain only one class attribute.

Working example:

<div :class="myClass" class="row">Test</div>

But is not possible to use two of the same style (i.e. two dynamic or two static)

Not working example:

<div class="a" class="b"></div> or <div :class="a" :class="b"></div>

This generates errors.

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

3 Comments

In my case the content of cssClass is dynamic, not the condition, like in your example obj.enabled.
you can use static, or dynamic with this syntax... please check I've updated the answer.
:class="[ { 'has-bomb': field.hasBomb && field.isOpen, 'is-open': field.isOpen, 'is-marked': field.isMarked, }, cssClass]" does the job. Thanks for pointing out!

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.