admin管理员组文章数量:1410705
I'm coordinating input elements with the keys of an object on Vuex state. Let's say I've got this object:
myObj: { foo: 1, bar: 2 }
And a puted property in a ponent:
myObjVals: {
get(){ return this.$store.state.myObj },
set(//?) { //? },
},
In the template, I can get values from the store:
<input type="number" v-model="myObjVals['foo']"/> // displays "1"
<input type="number" v-model="myObjVals['bar']"/> // displays "2"
But when I adjust the value on an input, I get the error: "Do not mutate vuex store state outside mutation handlers."
One obvious solution here is to have a different puted property for each key in myObj
... but for larger objects, this gets repetitious/cumbersome. I am wondering if there is any way to code this as I am attempting to above, that is, using only one puted property to reference the object for both get
and set
functions in v-model
.
I'm coordinating input elements with the keys of an object on Vuex state. Let's say I've got this object:
myObj: { foo: 1, bar: 2 }
And a puted property in a ponent:
myObjVals: {
get(){ return this.$store.state.myObj },
set(//?) { //? },
},
In the template, I can get values from the store:
<input type="number" v-model="myObjVals['foo']"/> // displays "1"
<input type="number" v-model="myObjVals['bar']"/> // displays "2"
But when I adjust the value on an input, I get the error: "Do not mutate vuex store state outside mutation handlers."
One obvious solution here is to have a different puted property for each key in myObj
... but for larger objects, this gets repetitious/cumbersome. I am wondering if there is any way to code this as I am attempting to above, that is, using only one puted property to reference the object for both get
and set
functions in v-model
.
-
1
I don't think that this is possible, because VueJS has no way of knowing that it should use the setter of the object instead of directly altering the object it has gotten from the state. One way could be to use the
mapState
of Vuex to help generate the needed getters (vuex.vuejs/guide/state.html#the-mapstate-helper). Edit: of course you could also use the non-strict mode of vuex – puelo Commented Jun 22, 2018 at 22:01 - Aha - this is a sort of pass-by-reference error, isn't is? – drenl Commented Jun 22, 2018 at 22:28
-
Will check out
mapState
, thanks. – drenl Commented Jun 22, 2018 at 22:28 - 1 Yeah. You are getting the whole object and by accessing the different properties inside your model you are altering the object without using the setter. At least i believe that this is the case. Have you tried using the setter and calling a Vuex mutation for the object from there? – puelo Commented Jun 22, 2018 at 22:31
-
1
That was were i am unsure of. Is the setter even called if you do the puted property on the whole object and then change property values inside the
v-model
. I would assume that the settter isn't even called. And i also believe that you cannot write a custom setter based on key. – puelo Commented Jun 23, 2018 at 16:36
1 Answer
Reset to default 6Come to think of your problem once more. One solution could be something suggested here: https://vuex.vuejs/guide/forms.html
<input type="number" :value="myObjVals['foo']" @input="changeMyObj('foo', $event)"/> // displays "1"
<input type="number" :value="myObjVals['bar']" @input="changeMyObj('bar', $event)"/> // displays "2"
with puted property and a method:
puted: {
myObjVals: function () {
return this.$store.state.myObj
}
},
methods: {
changeMyObj(key, evt) {
this.$store.mit('changeMyObj', {key: key, value: evt.data})
}
}
With a mutation inside the store:
mutations: {
changeMyObj(state, objInput) {
state.myObj[objInput.key] = objInput.value
}
}
Here is a "working" fiddle: http://jsfiddle/zfab6tzp/346/
Not sure if this is the best solution possible, but it seems to be working
本文标签: javascriptSpecify object value with vuex vmodelStack Overflow
版权声明:本文标题:javascript - Specify object value with vuex v-model - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744933957a2633067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论