0

I'm trying to set a class depending on if certain values are true.

My div:

<template>
<div class="navbar" v-bind:class=" {'test1': static, 'test2': isActive } ">
</template>

and in the script part of my component, I've written this:

<script>

export default {
  name: 'my-component',
  data: {
    static: true,
    isActive: true
  }
}

</script>

According to my understanding, this should show both the test1 class, and the test2 class. What am I misunderstanding -- shouldn't this be working? I want to toggle classes on click of a button, but I can't get this part working.

1
  • 1
    In a component, data needs to be a function. Commented Sep 18, 2017 at 19:26

1 Answer 1

1

Data needs to be a function in a component.

export default {
  name: 'my-component',
  data(){
    return {
      static: true,
      isActive: true
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thanks for the quick answer. This works well for my purposes -- thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.