Is it possible to use two-way computed properties with vuejs when using class-style components? In my case, given an app with a simple vuex store, is there a way to bind the store values to a select with v-model?
In the vuex documentation there is an example for two-way binding:
// ...
computed: {
message: {
get () {
return this.$store.state.obj.message
},
set (value) {
this.$store.commit('updateMessage', value)
}
}
}
But I did not get this to work with class components. I tried something like
private get myvalue(): boolean {
return store.state.myvalue;
}
/* Same type of function I would use when using @input one-way binding*/
private set myvalue(e: Event /* Wrong type for the setter... */) {
const target: HTMLSelectElement = e.target as HTMLSelectElement;
const value = target.value;
if (Boolean(value)) {
store.commit("myvalue", target.value);
}
}
But this obviously does not work because getters/setters always have the same data type. The type of myvalue is bool, but passing it to both functions does not work as well, because when binding to a select like so
<select v-model="myvalue">
<option value="false">No</option>
<option value="true">Yes</option>
</select>
The setter does not seem to get a value.
I also tried to bind the value to the select manually (like you would do with text-inputs) but using :selected on the select options didn't work (the first option always was selected).
myvalueis a bool then maybe you need to make sure the option values are also bools instead of strings by doing:value="false"and:value="true".