在使用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
当选择另一个值时,所选组件的值不会被更新。但是,v-select仍会显示新选择的值。
我什至尝试过各种“技巧”
set(val){
if (this.allowChanges){
console.log("updating")
this.selected = val
} else {
this.selected = this.selected
}
但没有运气。
我相信v-select会保持自己的内部选定值。
答案 0 :(得分:2)
我是用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
}
}
}
}
答案 1 :(得分:2)
这什么都不做:
this.selected = this.selected
您必须设置值,等待vue更新道具,然后再次将其重新设置:
const oldVal = this.selected
this.selected = val
this.$nextTick(() => {
this.selected = oldVal
})