admin管理员组文章数量:1400088
I have 3 inputs like this:
<input type="text" v-model="settings['apple']" />
<input type="text" v-model="settings['banana']" />
<input type="text" v-model="settings['orange']" />
When a user enter value for a input I want to get the value which user entered to process value and update new value. I am using puted properties to process value:
data() {
return {
settings: {}
}
},
puted: {
settings: {
set: function (newValue) {
var parts = newValue.match(/[\s\S]{1,2}/g) || [];
// Set new value ...
}
}
},
The problem is how can I know which input user entered and set the new value?
I have 3 inputs like this:
<input type="text" v-model="settings['apple']" />
<input type="text" v-model="settings['banana']" />
<input type="text" v-model="settings['orange']" />
When a user enter value for a input I want to get the value which user entered to process value and update new value. I am using puted properties to process value:
data() {
return {
settings: {}
}
},
puted: {
settings: {
set: function (newValue) {
var parts = newValue.match(/[\s\S]{1,2}/g) || [];
// Set new value ...
}
}
},
The problem is how can I know which input user entered and set the new value?
Share Improve this question edited Apr 18, 2016 at 8:17 user3118789 asked Apr 17, 2016 at 8:42 user3118789user3118789 6092 gold badges14 silver badges26 bronze badges 4- What does your getter look like? – nils Commented Apr 17, 2016 at 13:26
- @nils I don't have getter function. – user3118789 Commented Apr 18, 2016 at 7:44
-
How are you using
v-model="settings['apple']"
then? Are you mixing$data
properties with asetter
? – nils Commented Apr 18, 2016 at 8:00 - @nils I just update my $data properties above. Thank you. – user3118789 Commented Apr 18, 2016 at 8:17
2 Answers
Reset to default 4Attributes defined in data
and puted
should be mutually exclusive - defining the same attribute in both places is asking for trouble. Also, objects under data should have default values.
So, instead, have your puted return a different object which is all of the transformed values. Leave the settings that your inputs are bound to with v-model
alone. Then you can bind separately to the puted object and display it to your user if you like.
data() {
return {
settings: {
"apple": "",
"banana": "",
"orange": ""
}
}
},
puted: {
transformed_settings: function () {
/* create and return an object with transformed apple, banana, orange */
}
},
The easiest way is to declare apple, banana and Orange as data elements or variables. Define functions based on a user input under methods (Not puted) then use @change="CheckUserInput" property in the inputboxes
本文标签: javascriptVuejs Set value in computed propertiesStack Overflow
版权声明:本文标题:javascript - Vue.js Set value in computed properties - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744256248a2597495.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论