目标:字段c=字段a+字段b
方法1
直接使用Mustache(胡子表达式)
<body>
<div id="example" >
<input v-model="a" placeholder="edit me">
<input v-model="b" placeholder="edit me">
<p>Message is: {{ c=a+b }}</p>
<input type="button" value="submit" @click="submit()"/>
</div>
</body>
<script>
new Vue({
el:'#example',
data: {
a: 'name',
b: '123',
c:''
},
methods: {
submit: function () {
alert(this.c);
}
}
})
</script>
方法2
使用Computed
<body>
<div id="example" >
<input v-model="a" placeholder="edit me">
<input v-model="b" placeholder="edit me">
<p>Message is: {{ c1 }}</p>
<input type="button" value="submit" @click="submit()"/>
</div>
</body>
<script>
new Vue({
el:'#example',
data: {
a: 'name',
b: '123',
c:''
},
computed: {
c1: function () {
this.c = this.a + this.b;
return this.c;
}
},
methods: {
submit: function () {
alert(this.c);
}
}
})
</script>