首页 > 解决方案 > V-chip-group 值未定义

问题描述

我经常使用 Vuetify v-chip-group 遇到这个问题。

我正在使用 v-chip-group v-model,这是选定的芯片索引。

<v-chip-group
  v-model="selectedItem"
>

我将值设置为 -1,所以默认情况下没有选择 v-chip

        data() {
            return {
                selectedItem: -1,
            }
        }

The problem I encounter is when a v-chip is selected, it changes the value from its default, but when this v-chip item is deleted from the list, the selectedItem value becomes undefined.

在没有任何选定筹码的情况下,有没有办法将值恢复为 -1 ?

标签: vue.jsvuetify.js

解决方案


您可以查看该值然后将其重置,但我想知道您为什么想要这种行为?是不是因为您需要评估该值是否为 -1 以便您可以做其他事情?我之所以问是因为我觉得组件在取消选择时将其值设置为“未定义”是有意义的。

data() {
  return {
    selectedItem: -1
  };
},
watch: {
  selectedItem(v) {
    console.log(v);
    if (!v) {
      this.selectedItem = -1;
    }
  }
}

推荐阅读