<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<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="=" @click="calc">
<input type="text" v-model="result">
</div>
<script src="vue.js"></script>
<script>
var app = new Vue( {
el: '#app',
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;
}
/*巧妙方式,尽量不要使用
var codestr = 'parseInt(this.n1)' + this.opt + 'parseInt(this.n2)'
this.result = eval(codestr)*/
}
}
})
</script>
</body>
</html>
v-model的小案例-简易计算器
猜你喜欢
转载自blog.csdn.net/stphencurry/article/details/115054124
今日推荐
周排行