vue实力超简单的计算器
<div id="app">
<input type="text" v-model="n1">
<select v-model="obt">
<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>
var vm = new Vue({
el: '#app',
data: {
n1: 0, //input值默认为0
n2: 0,
result: 0,
obt: '+' //方法默认为+
},
methods: {
calc() { //计算器算术的方法
//逻辑:
switch (this.obt) {
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>