首页 > 解决方案 > 如何绕过 vuetify 的 v-select 内部状态

问题描述

我试图防止在使用 vuetify 的 v-select 组件时“选择”一个值。

鉴于:

<v-checkbox
    v-model="allowChanges"
></v-checkbox>
<v-select
    v-model="twoWayComputed"
    :items="items"
></v-select>

new Vue({
  el: '#app',
  data: () => ({
    selected: "Foo",
    allowChanges: false,
    items: ['Foo', 'Bar', 'Fizz', 'Buzz']
  }),
  computed: {
    twoWayComputed: {
      get(){
        return this.selected
      },
      set(val){
        if (this.allowChanges){
          console.log("updating")
          this.selected = val
        }
      }
    }
  }
})

Codepen:https ://codepen.io/anon/pen/mYNVKN?editors=1011

When another value is selected, the components selected value is not being updated. 但是 v-select 仍然显示新的选定值。

我什至尝试了各种“技巧”,比如

  set(val){
    if (this.allowChanges){
      console.log("updating")
      this.selected = val
    } else {
      this.selected = this.selected
    }

但没有运气。

我相信 v-select 正在维护自己的内部选择值。

标签: javascriptvue.jsvuetify.js

解决方案


我使用slot-scope外观制作它:

<v-select
  v-model="twoWayComputed"
  :items="items"
  label="scoped"
>
  <template slot="selection" slot-scope="data">
    {{ selected }}
  </template>
  <template slot="item" slot-scope="data">
    {{ data.item }}
  </template>
</v-select>

  data: () => ({
    selected: "Foo",
    allowChanges: false,
    items: ['Foo', 'Bar', 'Fizz', 'Buzz']
  }),
  computed: {
    twoWayComputed: {
      get(){
        return this.selected
      },
      set(val) {
        if (this.allowChanges) {
          console.log("updating")
          this.selected = val
        } 
      }
    }
  }

签出我的代码笔


推荐阅读