admin管理员组文章数量:1336340
So I have this basic VUE.js page, but I want variable C to be a posite of variable A and B together.
var app = new Vue({
el: '#app',
data: {
firstname: '',
lastname: '',
fullname: firstname + lastname
}
})
so I can use it in a <input type="text" v-model="fullname">
and it will contain the values of firstname and lastname.
firstname and lastname will be binded to 2 other inputs as follows:
<label>Insert your first name:</label>
<input type="text" v-model="firstname">
<label>Insert your last name:</label>
<input type="text" v-model="lastname">
so the fullname variable will be a dynamically binded firstname + lastname variable.
I've tried several things but I can't seem to get this working.
So I have this basic VUE.js page, but I want variable C to be a posite of variable A and B together.
var app = new Vue({
el: '#app',
data: {
firstname: '',
lastname: '',
fullname: firstname + lastname
}
})
so I can use it in a <input type="text" v-model="fullname">
and it will contain the values of firstname and lastname.
firstname and lastname will be binded to 2 other inputs as follows:
<label>Insert your first name:</label>
<input type="text" v-model="firstname">
<label>Insert your last name:</label>
<input type="text" v-model="lastname">
so the fullname variable will be a dynamically binded firstname + lastname variable.
I've tried several things but I can't seem to get this working.
Share Improve this question asked Aug 13, 2017 at 21:40 KevinKevin 9791 gold badge8 silver badges14 bronze badges 3- Do you want the fullname to be an input? – Bert Commented Aug 13, 2017 at 21:41
- Check: Computed Properties – Gerardo Commented Aug 13, 2017 at 21:43
-
2
The reason I ask is because typically you will do this with a puted property, but if you bind it to an
input
what do you expect to happen when the user changes the fullname input. Do you expect the value to be split out into first name and last name and how do you expect to acplish that? By spaces? Etc. – Bert Commented Aug 13, 2017 at 21:45
3 Answers
Reset to default 8Use puted properties.
var app = new Vue({
el: '#app',
data: {
firstname: '',
lastname: ''
},
puted: {
fullname: function(){
return this.firstname + ' ' + this.lastname;
}
}
});
You have a property that is dependent on other properties. So use puted.
var app = new Vue({
el: '#app',
data: {
firstname: '',
lastname: ''
},
puted: {
fullname: function () {
return `${this.firstname} ${this.lastname}`
}
}
})
There is this exact example in the doc.
Working code snippet, with some additional checks, so the fullname
is editable too:
var app = new Vue({
el: '#app',
data: {
firstname: 'Foo',
lastname: 'Bar'
},
puted: {
fullname: {
// getter
get: function () {
return this.lastname ? this.firstname + ' ' + this.lastname : this.firstname
},
// setter
set: function (newValue) {
if (newValue.indexOf(' ') !== -1) {
var names = newValue.split(' ')
this.firstname = names[0]
this.lastname = names[1] ? names[1] : ''
} else {
this.firstname = newValue
}
}
}
}
})
<div id="app">
<label>Insert your first name:</label>
<input type="text" v-model="firstname">
<label>Insert your last name:</label>
<input type="text" v-model="lastname">
<label>Insert your full name:</label>
<input type="text" v-model="fullname">
</div>
<script src="https://unpkg./[email protected]/dist/vue.min.js"></script>
本文标签: javascriptVueJS data object is 2 data objects combinedStack Overflow
版权声明:本文标题:javascript - VueJS data object is 2 data objects combined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742381842a2464249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论