如何在vue js中切换多个元素的类

Nit*_*mar 0 vue.js vuejs2

我正在尝试切换多个元素的类Vuejs 2.0,我有以下一组按钮,它们有一个类btn-primary.单击按钮可显示该特定元素的子组.这是我的代码:

<button class="btn btn-primary btn-xs" v-on:click.prevent="getTags('investor')">Investor</button>
<button class="btn btn-primary btn-xs" v-on:click.prevent="getTags('research')">Research</button>
<button class="btn btn-primary btn-xs" v-on:click.prevent="getTags('company')">Company</button>
Run Code Online (Sandbox Code Playgroud)

这显示以下元素:

<div v-if="tag.investor">
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Investor - Mutual Funds')">Mutual Fund</button>
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Investor - Insurance')">Insurance</button>
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Investor - FII')">FII</button>
</div>
<div v-if="tag.research">
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Research - Tier I')">Research - Tier I</button>
    <button class="btn btn-primary btn-xs" v-on:click.prevent="selectTags('Research - Tier II')">Research - Tier II</button>
</div>
Run Code Online (Sandbox Code Playgroud)

我有以下内容data():

tag: {
    investor: false,
    research: false,
    company: false,
    others: false,
 },
Run Code Online (Sandbox Code Playgroud)

并在methods:

getTags: function (tag) {
    this.tag.investor = this.tag.research = this.tag.company = this.tag.others = false
    if(tag == 'investor')
    {
        this.tag.investor = true
    }
    if(tag == 'research')
    {
        this.tag.research = true
    }
    if(tag == 'company')
    {
        this.tag.company = true
    }
    if(tag == 'others')
    {
        this.tag.others = true
    }
},
Run Code Online (Sandbox Code Playgroud)

我希望有一个类,btn-warningbtn-primary在选择任何子元素后删除.任何想法如何实现这一点,我不希望有个别数据元素和切换类.

Ber*_*ert 6

我想为你的Vue建议一种数据驱动的方法.考虑这个数据结构:

const tags = {
   Investor:[
     {display:"Mutual Fund", value:"Investor - Mutual Funds"},
     {display:"Insurance", value:"Investor - Insurance"},
     {display:"FII", value:"Investor - FII"},
   ],
   Research:[
     {display:"Research - Tier I", value:"Research - Tier I"},
     {display:"Research - Tier II", value:"Research - Tier II"},
  ]
}
Run Code Online (Sandbox Code Playgroud)

如果使用它,那么您可以大大清理模板并处理添加到数据结构中的任何其他标记.

<div id="app">
  <button v-for="(obj, key) in tags"
          :key="key"
          @click="currentTag = key"
          class="btn btn-xs btn-primary">
    {{key}}
  </button>

  <div>  
    <button v-for="tag in tags[currentTag]"
            :key="tag"
            class="btn btn-xs"
            :class="tagClass(tag)"
            @click="selectTags(tag.value)">
      {{tag.display}}
    </button>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

你的Vue看起来也更清洁了.

new Vue({
  el:"#app",
  data:{
    currentTag: null,
    tags,
    selectedTags:[]
  },
  methods:{
    selectTags(tag){
      const index = this.selectedTags.findIndex(t => t == tag)

      if (index >= 0)
        this.selectedTags.splice(index, 1)
      else
        this.selectedTags.push(tag)
    },
    tagClass(tag){
      const isSelected = this.selectedTags.includes(tag.value)

      return {
        'btn-warning': isSelected,
        'btn-primary': !isSelected
      }
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

这是一个例子.