小编典典

单击Vue.js切换类

css

如何在vue.js中切换类?

我有以下几点:

<th class="initial " v-on="click: myFilter">
    <span class="wkday">M</span>
</th>

new Vue({
  el: '#my-container',
  data: {},
  methods: {
    myFilter: function(){
      // some code to filter users
    }
  }
});

当我单击时,th我想按以下方式申请active课程:

<th class="initial active" v-on="click: myFilter">
    <span class="wkday">M</span>
</th>

这需要切换,即每次单击它都需要添加/删除类。


阅读 302

收藏
2020-05-16

共1个答案

小编典典

您可以让活动类依赖于布尔数据值:

<th 
  class="initial " 
  v-on="click: myFilter"
  v-class="{active: isActive}">
  <span class="wkday">M</span>
</th>

new Vue({
  el: '#my-container',

  data: {
    isActive: false
  },

  methods: {
    myFilter: function() {
      this.isActive = !this.isActive;
      // some code to filter users
    }
  }
})
2020-05-16