<div id="app">
<h4>{{ msg }}</h4>
<!--v-bind只能实现数据的单向绑定,从M自动绑定到V,无法实现数据的双向绑定-->
<input type="text" v-bind:value="msg">
</div>
<script>
//创建Vue实例,得到ViewModel
var vm= new Vue({
el: '#app',
data: {
msg: '爱学习,爱思考'
},
methods: {
}
});
</script>
<div id="app">
<h4>{{ msg }}</h4>
<!--v-bind只能实现数据的单向绑定,从M自动绑定到V,无法实现数据的双向绑定-->
<!--<input type="text" v-bind: value="msg">-->
<!--使用v-model指令,可以实现表单元素和Model中数据的双向数据绑定-->
<!--注意:v-model只能运用在表单元素中-->
<!--如input(radio, text, address, email...) select checkbox textarea-->
<input type="text" style="width:100%;" v-model="msg">
</div>
<script>
//创建Vue实例,得到ViewModel
var vm= new Vue({
el: '#app',
data: {
msg: '爱学习,爱思考'
},
methods: {
}
});
</script>
简易计算器
<div id="app">
<input type="text" v-model="n1">
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" v-model="n2">
<input type="button" value="=" @click="calc">
<input type="text" v-model="result">
</div>
<script>
//创建Vue实例,得到ViewModel
var vm= new Vue({
el: '#app',
data: {
n1: 0,
n2: 0,
result: 0,
opt: '+'
},
methods: {
calc(){//计算器算术的方法
//逻辑:
switch (this.opt){
case '+':
this.result = parseInt(this.n1)+parseInt(this.n2)
break;
case '-':
this.result = parseInt(this.n1)-parseInt(this.n2)
break;
case '*':
this.result = parseInt(this.n1)*parseInt(this.n2)
break;
case '/':
this.result = parseInt(this.n1)/parseInt(this.n2)
break;
}
}
}
});
</script>