Vue利用普通方法或计算属性或侦听属性实现简易计算器

vue实现简单计算机(三种方式)

1.使用普通方法

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
  <script src="js/vue.js" type="text/javascript"></script>
 </head>
 <body>
  <div id="app">
   <input type="number" placeholder="请输入数字" v-model.number="number1" />
   <select v-model="pwd">
    <option>请选择符号</option>
    <option value="+">+</option>
    <option value="-">-</option>
    <option value="*">*</option>
    <option value="/">/</option>
   </select>
   <input type="number" placeholder="请输入数字" v-model.number="number2" />
   <button @click="pd()">等于</button>
   <input type="text" v-model="result" placeholder="结果"/>
   <br />
   
  </div>
 </body>
 <script>
  new Vue({
   el:"#app",
   data:{
    number1:'',
    number2:'',
    pwd:'请选择符号',
    result:''
   },
   methods:{
    pd(){
     switch(this.pwd){
      case '+':
       this.result = this.number1+this.number2;
       break;
      case '-':
       this.result = this.number1-this.number2;
       break;
      case '*':
       this.result = this.number1*this.number2;
       break;
      case '/':
       this.result = this.number1/this.number2;
       break;
     }
    }
   }
  });
 </script>
</html>

二、通过computed计算属性
将计算结果设定为一个computed计算属性,使用eval和模板字符串计算表达式的值

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/vue.js"></script>
	</head>
	<body>
		<div id="app">
			<input id="input" type="number" v-model="num1" placeholder="请输入数字" />
			 <select v-model="selected">
				<option value ="">请选择</option>
			 	<option value ="+">+</option>
				<option value ="-">-</option>
				<option value ="*">*</option>
				<option value ="/">/</option>
				<option value ="%">%</option>
			 </select>
			<input type="number" v-model="num2" placeholder="请输入数字"/>
			<input type="text" v-model="result" onfocus="this.blur()" placeholder="请输入完整的表达式"/>
		</div>
		<script type="text/javascript">
			var vm = new Vue({
				el:'#app',
				data:{
					num1:0,//第一输入框中的数字
					num2:0,//第二输入框中的数字
					selected:''//默认选择的运算符 可以设置为 '','+','-','*','/','%'
				},
				computed:{
					result:function(){
						if(this.selected === '' || this.num1=='' || this.num2 =='') //表达式不完整时跳过运算
							return null
						return eval(`${this.num1}${this.selected}(${this.num2})`)
					}
				}
			})
		</script>
	</body>
</html>

三、使用watch侦听属性
Vue 提供了一种更通用的方式来观察和响应 Vue 实例上的数据变动:侦听属性。
每当侦听的属性发生改变时,vue会执行该属性对应的函数。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>watch实现</title>
  <script src="js/vue.js" type="text/javascript"></script>
 </head>
 <body>
  <div id="app">
   <input type="text" placeholder="请输入数字" v-model.number="number1" />
   <select v-model="pwd">
    <option value="+">+</option>
    <option value="-">-</option>
    <option value="*">*</option>
    <option value="/">/</option>
   </select>
   <input type="text" placeholder="请输入数字" v-model.number="number2" />
   <button @click="pd()">等于</button>
   <input type="text" v-model="result"/>
   <br />
   
  </div>
 </body>
 <script>
  var vm = new Vue({
   el:"#app",
   data:{
    number1:0,
    number2:0,
    pwd:"+",
    result:0
   },
   computed:{
    add(){
     var num1 = this.number1
     var num2 = this.number2
     var num3 = this.pwd
     return{
      num1,
      num2,
      num3
     }
    }
   },
   watch:{
    add(val){
     switch(this.pwd){
      case '+':
       this.result = this.number1+this.number2;
       break;
      case '-':
       this.result = this.number1-this.number2;
       break;
      case '*':
       this.result = this.number1*this.number2;
       break;
      case '/':
       this.result = this.number1/this.number2;
       break;
     }
    },
    
   }
  });
   
 </script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以帮你解答这个问题。 首先,你需要在组件中定义一个计算属性来计算结果。假设你想要实现加法、减法、乘法和除法,可以这样写: ```js computed: { result() { switch (this.operator) { case '+': return this.firstNumber + this.secondNumber; case '-': return this.firstNumber - this.secondNumber; case '*': return this.firstNumber * this.secondNumber; case '/': return this.firstNumber / this.secondNumber; default: return 0; } } } ``` 然后,在模板中,你可以使用 v-model 指令绑定输入框的值,并使用 v-on 指令绑定按钮的点击事件,将用户输入的值和操作符传递给计算属性,最后将计算结果显示出来。代码如下: ```html <template> <div> <input type="number" v-model="firstNumber"> <select v-model="operator"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="number" v-model="secondNumber"> <button @click="calculate">计算</button> <p>结果:{{ result }}</p> </div> </template> <script> export default { data() { return { firstNumber: 0, secondNumber: 0, operator: '+' } }, computed: { result() { switch (this.operator) { case '+': return this.firstNumber + this.secondNumber; case '-': return this.firstNumber - this.secondNumber; case '*': return this.firstNumber * this.secondNumber; case '/': return this.firstNumber / this.secondNumber; default: return 0; } } }, methods: { calculate() { // 什么也不做,计算属性会自动更新 } } } </script> ``` 这样,你就可以在 Hello Vue 组件中制作一个简易计算器了。注意,这个计算器没有做任何输入验证和异常处理,只是为了演示计算属性的用法。如果你想要使用这个计算器,需要自行添加输入验证和异常处理的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值