W
W
webhero2021-03-16 15:51:14
Vue.js
webhero, 2021-03-16 15:51:14

How to change the class on click for each element separately?

Started learning Vue
I got to a certain point when the color switches, but for all objects at once and wondered how to switch for each separately? Specified active: true in each object, but it did not help

<template>
  <main>
      <ul>
          <li v-bind:class="{'greenlight': active}" v-for="data in webdata" :key="data.key" @click="data.show = !data.show" v-on:click="active = !active">
            <h3 v-show="data.show">{{data.name}}</h3>
          </li>
      </ul>
  </main>
</template>


export default {
  data () { 
    return {
      active: true,
      webdata: [
        {name: 'JavaScript', show: false},
        {name: 'Vue.js', show: false}, 
        {name: 'CSS', show: false}, 
        {name: 'HTML', show: false}
      ]
    }
  }
}

.greenlight {
  background: lightgreen;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2021-03-16
@webhero

You can check for activity not through the boolean flag, but set for example selected = data.key and the class then check
:class = "{'greenlight':selected == data.key}"

T
tantumus21, 2021-03-16
@tantumus21

Use components . Through components you create your own component. And each such component in the list will be, as it were, a separate application with its own configuration.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question