使用Vue编写简单计算器
完整代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
<style type="text/css">
#app{
height: 120px;
width: 200px;
margin: 0 auto;
border: 2px solid red;
}
</style>
</head>
<body>
<div id="app">
<input type="text" name="" id="" value="" v-model.number="number1" />
<select v-model="opt">
<option >+</option>
<option >-</option>
<option >*</option>
<option >/</option>
</select>
<input type="text" name="" id="" value="" v-model.number="number2" /><br />
<button @click="change">计算</button><br>
结果:{{jg}}
</div>
</body>
<script type="text/javascript">
var vm = new Vue({
el:"#app",
data:{
number1:0,
number2:0,
opt:"",
jg:0
},
methods:{
change(){
if(this.opt=="+"){
this.jg=parseInt(this.number1)+parseInt(this.number2);
}else if(this.opt=="-"){
this.jg=parseInt(this.number1)-parseInt(this.number2);
}else if(this.opt=="*"){
this.jg=parseInt(this.number1)*parseInt(this.number2);
}else{
this.jg=parseInt(this.number1)/parseInt(this.number2);
}
}
}
})
</script>
</html>
运行结果:
程序分析:
在Vue中,v-model 指令,可以实现表单元素和 Model 中数据的双向数据绑定,接下来,我们就用这个指令编写一个简单的计算器,代码如下
1.设定两个可供输入的数值,并设定双向绑定机制
<input type="text" name="" id="" value="" v-model.number="number1" />
<input type="text" name="" id="" value="" v-model.number="number2" />
ps:v-model.number是将绑定类型转化为number格式。
2.设定四个可选的标点符号
<select v-model="opt">
<option >+</option>
<option >-</option>
<option >*</option>
<option >/</option>
</select>
3.设定运行案件,开启启动程序,并绑定运算程序
<button @click="change">计算</button>
结果:{{jg}}
运算方案:
方法1:
var vm = new Vue({
el:"#app",
data:{
number1:0,
number2:0,
opt:"",
jg:0
},
methods:{
change(){
if(this.opt=="+"){
this.jg=parseInt(this.number1)+parseInt(this.number2);
}else if(this.opt=="-"){
this.jg=parseInt(this.number1)-parseInt(this.number2);
}else if(this.opt=="*"){
this.jg=parseInt(this.number1)*parseInt(this.number2);
}else{
this.jg=parseInt(this.number1)/parseInt(this.number2);
}
}
}
})
输入框中的值都是字符串形式,需要使用 parseInt() 把字符串转换成数值型,也可以使用 vue 提供的 number 修饰符
方法2:
var vm = new Vue({
el: '#app',
data: {
number1: 0,
number2: 0,
jg: 0,
opt: ''
},
methods: {
calc() { // 计算器算数的方法
// 逻辑:
switch (this.opt) {
case '+':
this.jg= parseInt(this.number1) + parseInt(this.number2)
break;
case '-':
this.jg= parseInt(this.number1) - parseInt(this.number2)
break;
case '*':
this.jg= parseInt(this.number1) * parseInt(this.number2)
break;
case '/':
this.jg= parseInt(this.number1) / parseInt(this.number2)
break;
}
}
}
});
两个方法效果无差别
其他运算方案:
使用watch监听器 或者 使用计算computed属性也可实现本方法
computed
computed:{
result(){
switch (this.opt) {
case '+':
return Number(this.n1) + Number(this.n2);
break;
case '-':
return Number(this.n1) - Number(this.n2);
break;
case '*':
return Number(this.n1) * Number(this.n2);
break;
case '/':
return Number(this.n1) / Number(this.n2);
break;
}
}
}
watch
methods:{
watch1(){
switch (this.opt) {
case '+':
this.result = Number(this.n1) + Number(this.n2);
break;
case '-':
this.result = Number(this.n1) - Number(this.n2);
break;
case '*':
this.result = Number(this.n1) * Number(this.n2);
break;
case '/':
this.result = Number(this.n1) / Number(this.n2);
break;
}
}
},
watch:{
n1:function(){
this.watch1()
},
n2:function(){
this.watch1()
},
opt:function(){
this.watch1()
}
},
created:{
this.watch1()
}