0

I want to write in less code a function that will add the active classname and automatically removes all the other active class names. But there is also a unique class name needed for JavaScript in my case. But want to put that all in class name. How can I make this a valid classname. Is there a way to do that so it will not conflict with each other.

    <ul class="three">
      <li 
        v-for="(post, index) in listData.data" 
        :key="index" 
        :class="'list-item unordered-list ' + post.name.toLowerCase() +  { active : activeName == post.name}" 
         @click="showInfo(post.name, post.description)">
        {{ post.name }}
      </li>
    </ul>

2 Answers 2

1

Have a look at the object syntax or array syntax of class binding, which allows binding to an object or array returned by a value. That way you can simplify complex class or style combinations by calling a function from the template, like the example from the docs:

<div v-bind:class="classObject"></div>
...
data: {
  isActive: true,
  error: null
},
computed: {
  classObject: function () {
    return {
      active: this.isActive && !this.error,
      'text-danger': this.error && this.error.type === 'fatal'
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

    <ul class="three">
      <li 
        v-for="(post, index) in listData.data" 
        :key="index" 
        :class="['list-item', 'unordered-list ', post.name.toLowerCase(), { active: activeName == post.name }]"
         @click="showInfo(post.name, post.description)">
        {{ post.name }}
      </li>
    </ul>

Comments

Your Answer

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