Vue实现简单计算器
v-model双向绑定
修饰符:number
watch监听器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model.number="value1" />
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" v-model.number="value2" />
<input type="button" value="=" @click="mathFunc" />
<input type="text" v-model="result" />
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
value1: '',
value2: '',
opt: '+',
result: ''
},
methods: {
calc() {
switch (this.opt) {
case '+':
this.result = this.value1 + this.value2
break
case '-':
this.result = this.value1 - this.value2
break
case '*':
this.result = this.value1 * this.value2
break
case '/':
this.result = this.value1 / this.value2
break
default:
this.result = 0
}
},
mathFunc() {
this.calc();
}
},
watch: {
opt() {
this.calc()
}
},
})
</script>
</body>