【发布时间】:2019-05-13 15:36:26
【问题描述】:
我有这个 vue 组件:
<template>
<div id="OrderTypeSelect" class="order-type-select">
<b-form-select v-model="curDocType" :options="options" class="mb-3">
</b-form-select>
</div>
</template>
选择输入的值绑定到 Vuex 存储,如下所示:
computed: {
curDocType: {
get () {
return this.$store.state.curDocType
},
set (value) {
this.$store.commit('setcurDocType', value)
}
}
}
我想不通的是如何有条件地防止选择值发生变化。我试过这个:
computed: {
curDocType: {
get () {
return this.$store.state.curDocType
},
set (value) {
if (this.$store.state.curOrder == ""){
this.$store.commit('setcurDocType', value)
}
else{
console.log("Debe descartar");
this.curDocType.get() //This throws error!
}
}
}
}
即使我不将值提交到商店,输入字段中的值也会更改。
当我的条件被触发时,我需要再次调用get()(或其他方式)以使此绑定持久:
if (this.$store.state.curOrder != "") {
//Do not modify store and return the input selection to it's previous value
}
【问题讨论】:
-
你试过
reurn this.$store.state.curDocType而不是this.curDocType.get() -
`return this.$store.state.curDocType 不行,输入值就这样改了。不知道 set() 是否应该返回一个值。无论如何都不是根据文档:vuex.vuejs.org/guide/forms.html#two-way-computed-property
标签: javascript vue.js vuejs2 vuex bootstrap-vue