<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div id="app">
<h6>{{message}}</h6>
<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 src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
var vm = new Vue({
el:"#app",
data: {
message:"计算",
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;
}
}
},
})
// this.result = eval('parseInt(this.n1)'+ this.opt + 'parseInt(this.n2)');
</script>
</body>
</html>
v-model
最新推荐文章于 2025-06-11 20:51:47 发布