vue设置一个简单的计算器
<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="=">
<input type="text" v-model="result">
<el-button @click="calc">计算</el-button>
</div>
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;
}
}
}
效果图: