<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简易计算器</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<!-- v-model:双向绑定事件-->
<input type="text" v-model="num1">
<select v-model="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" v-model="num2">
<!-- v-on用于事件(如click)的监听绑定-->
<button v-on:click="cal">=</button>
<input type="text" v-model="result">
</div>
<script>
var vm = new Vue({
el:"#app",
data:{
num1:0,
num2:0,
result:0,
opt:"+"
},
methods:{
cal(){
switch (this.opt) {
case "+":
// parseInt() 函数可解析一个字符串,并返回一个整数
this.result = parseInt(this.num1) + parseInt(this.num2)
break;
case "-":
this.result = parseInt(this.num1) - parseInt(this.num2)
break;
case "*":
this.result = parseInt(this.num1) * parseInt(this.num2)
break;
case "/":
this.result = parseInt(this.num1) / parseInt(this.num2)
break;
}
}
}
})
</script>
</body>
</html>
vuejs基础-计算器案例