<div id="demo">
姓: <input type="text" placeholder="First Name" v-model="firstName"><br>
名: <input type="text" placeholder="Last Name" v-model="lastName"><br>
姓名 1(单向): <input type="text" placeholder="Full Name" v-model="fullName1"><br>
姓名 2(单向): <input type="text" placeholder="Full Name" v-model="fullName2"><br>
姓名 3(双向): <input type="text" placeholder="Full Name2" v-model="fullName3"><br>
</div>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
var vm = new Vue({
el: ‘#demo‘,
data: {
firstName: ‘Kobe‘,
lastName: ‘bryant‘,
fullName2: ‘Kobe bryant‘
},
computed: {
fullName: function () {
return this.firstName + " " + this.lastName
},
fullName3: {
get: function () {
return this.firstName + " " + this.lastName
},
set: function (value) {
var names = value.split(‘ ‘)
this.firstName = names[0]
this.lastName = names[1]
}
}
},
watch: {
lastName: function (newVal, oldVal) {
this.fullName2 = this.firstName + ‘ ‘ + newVal
}
}
})
vm.$watch(‘firstName‘, function (val) {
this.fullName2 = val + ‘ ‘ + this.lastName
})
原文:https://www.cnblogs.com/piao-bo/p/13512009.html