Dynamically add Class in Vue

there is a v-for loop list in the project, and the loop object in the computed cycle
now wants to add a click event to the list, adding a class, when clicked, but does not immediately succeed (update dom? Other events, if any, have to be triggered elsewhere before there is a response.

the following code is attached:
html:

<div class="col-5" v-for="(item,index) in arr">
      <p v-bind:class="{"selectActive":item.checked}" v-on:click="isActive(item)" :key="item.id">{{item.text}}

</div> </div>

js:

export default {
    name: "App2",
    data () {
        return {
            //arr
        }
    },
    methods:{
        isActive:function(item,status){
            console.log(item.checked)
            if(typeof item.checked == "undefined") {
                this.$set(item,"checked",true);
            } else {
                item.checked = !item.checked;
            }
            console.log(item.checked)
        },
    },
    computed:{
        arr:function(){
            return [
              {text:"A"},
              {text:"B"},
              {text:"C"},
              {text:"D"}
            ]
        },
    }
}

css:

.col-5 {
    float:left;
    width: 25%;
    color:-sharpfff;
}
.col-5 p {
    background: -sharp000;
    line-height: 50px;
    text-align: center;
}
.col-5 p.selectActive {
    background: -sharpf00;
}

ask the bosses for advice.

Feb.10,2022

you call this.$set (item,'checked',true) directly; the view update cannot be triggered, because the value of the arr in your computed is not the data in the data, so you can trigger the view update manually.

      isActive:function(item,status){
          console.log(item.checked)
          if(item.checked == undefined) {
              item.checked = true
          } else {
              item.checked = !item.checked
          }
          this.$forceUpdate()
          console.log(item.checked)
      },

it is recommended that you write arr in data.


data:
data () {
     return {
         arr: [
            {text:"A",checked:false},
            {text:"B",checked:false},
            {text:"C",checked:false},
            {text:"D",checked:false}
         ]
        }
  },
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb5659-254f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb5659-254f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?