【问题标题】:How can I clear active item in Vue v-list?如何清除 Vue v-list 中的活动项?
【发布时间】:2020-02-01 22:38:09
【问题描述】:

在 Vuetify 的 v-list-item 指令中,一旦选择了一个项目,我就无法删除活动道具。到目前为止,这是我尝试过的:

      <v-list-item-group pb-6 color="primary" class="pb-3 text-left">
         <v-list-item v-for="(answer, index) in answers" :key="index">
            <v-list-item-title :class="{ active: isActive }" v-text="answer" @click.prevent="selectAnswer(index)">
            </v-list-item-title>
         </v-list-item>
      </v-list-item-group>

<b-button variant="primary" :disabled="hasAnswered" @click="submitAnswer()">
Submit
</b-button>
    export default {
  name: 'QuestionBox',
  data () {
    return {
      answers: '',
      selectedIndex: null,
      hasAnswered: false,
      isActive: undefined
    }
  },
  methods: {
    selectAnswer (ind) {
      this.isActive = true
      this.selectedIndex = ind
    },
    submitAnswer () {
      this.hasAnswered = true
      this.isActive = false
    }
  }
}

我知道:class="{ active: isActive }" 将适用于 v-list-item-title,但 v-list-item 有 v-for,所以有人有什么想法吗?

【问题讨论】:

    标签: javascript vue.js vue-component vuetify.js


    【解决方案1】:

    您可以使用v-model 来做到这一点

    从示例中调用clear 方法将清除选择

    示例(单选):

    <v-list-item-group v-model="selection">
    
    data: () => ({
      selection: null
    }),
    methods:{
      clear(){ this.selection = null }
    }
    

    示例(多选):

    <v-list-item-group v-model="selection" multiple>
    
    data:() => ({
      selection: []
    }),
    methods:{
      clear(){ this.selection = [] }
    }
    

    【讨论】:

    • 谢谢,这行得通,你能告诉我为什么 v-model 能够解决这个问题吗?
    • Model 表示具有 2 向绑定的选择。所以如果你更新模型,选择就会改变。
    猜你喜欢
    • 2012-05-15
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 2020-11-07
    • 2022-11-22
    • 1970-01-01
    • 2011-05-10
    • 2023-03-08
    相关资源
    最近更新 更多