简体   繁体   中英

How to get around vuetify's v-select internal state

I'm trying to prevent a value from being 'selected' when using vuetify's v-select component.

Given:

<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. However v-select still shows the new selected value.

I even tried all kinds of "tricks" like

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

but no luck.

I believe v-select is maintaining its own internal selected value.

I made it using slot-scope look:

<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
        } 
      }
    }
  }

check-out-my-codepen

This does nothing:

this.selected = this.selected

You have to set the value, wait for vue to update the props, then set it back again:

const oldVal = this.selected
this.selected = val
this.$nextTick(() => {
  this.selected = oldVal
})

Codepen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM