Vue.js 当用户取消全部选择时,Vue watcher是否选中全部复选框?
我使用数组Vue.js 当用户取消全部选择时,Vue watcher是否选中全部复选框?,vue.js,Vue.js,我使用数组['one'、'two'、'three']创建组合框并与v-modelselectValue绑定,然后在该v-model上添加watcher,以便在用户取消选择所有选项时选中所有复选框 watch: { selectValue(newVal) { if(newVal.length===0) this.selectValue = ['one', 'two', 'three'] } } 当我注销该值时。它显示selectValue值包含三个值['one','
['one'、'two'、'three']
创建组合框并与v-modelselectValue
绑定,然后在该v-model上添加watcher,以便在用户取消选择所有选项时选中所有复选框
watch: {
selectValue(newVal) {
if(newVal.length===0)
this.selectValue = ['one', 'two', 'three']
}
}
当我注销该值时。它显示selectValue
值包含三个值['one','two','three']
,但在UI中最后一个取消选择复选框未被选中,但当我使用setTimeout
环绕assign语句时,它会工作(没有超时)。
有没有其他方法可以做到这一点。我应该像当前一样使用
setTimeout
吗?您可以将代理计算值与setter一起使用:
data: () => ({
items: ['foo', 'bar', 'fizz', 'buzz'],
value : []
}),
computed : {
_value : {
get () {
return this.value
},
set (val) {
this.value = val.length ? val : this.items
},
}
}
然后将v型模型链接到代理计算值:
<v-select
v-model="_value"
:items="items"
multiple
></v-select>
工作示例:尝试
this.$set(this,'selectValue',['one','two','three'])
代替this.selectValue=['one','two','three']
@ShivamSingh它不工作,但谢谢