0

I'm trying to add a class to my bind however im getting an error on the "+ animation" its saying : is expected, how do i go about getting that class

class="animation__placeholder"
:class="{
  'animation__placeholder--' + animation,
  'animation__placeholder--top':(alignment === 'top'),
  'animation__placeholder--center':(alignment === 'center'),
  'animation__placeholder--bottom':(alignment === 'bottom'),
  'animation__placeholder--right':(side === 'right'),
  'animation__placeholder--center':(side === 'center'),
  'animation__placeholder--left':(side === 'left'),
}"
1
  • 1
    The text inside `:class="..." is JS code. This object literal syntax is invalid. It's not specific to Vue. Commented Oct 25, 2022 at 9:40

2 Answers 2

1

In your code, you're doing things complex. Here is the simpler one.

new Vue({
  el: '#app',
  data() {
    return {
      animation: 'fade-out',
      alignment: 'center',
      side: 'right'
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <section 
    class="animation__placeholder"
    :class="`
      animation__placeholder--${animation}
      animation__placeholder--${alignment}
      animation__placeholder--${side}
    `"
  >Devtools</section>
</div>

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

Comments

0

You can bind multiple classes as a list.

:class="['A B', condition1 ? 'C' : 'D', condition2 && 'E']"

Assuming that condition1 evaluates to false and condition2 to true, the resulting component would have classes 'A B D E'.

Now, nothing is stopping you from putting in functions in there from which you return a class name, which is probably a preferred method considering you shouldn't be putting too much login into templates anyway.

With that said, I think that @Amini's solution is very elegant and probably a better fit for your use case.

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.