vue计算的四种写法

1.用方法的方式写:两个input一个option用v-model实现双向绑定,oncilck点击触发showinfo方法,switch判断计算符号

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script type="text/javascript" src="js/vue.js"></script>
	</head>
	<body>
		<div id="app">
			<input type="text" v-model.number="number1" />
			<select v-model="opt">
				<option>+</option>
				<option>-</option>
				<option>*</option>
				<option>/</option>
			</select>
			<input type="text" v-model.number="number2" />
			<button @click="showInfo">计算</button>
			<h2>方法:{{val}}</h2>
		</div>
	</body>
	<script type="text/javascript">
		var vm = new Vue({
			el: "#app",
			data: {
				number1: 0,
				number2: 0,
				val: "",
				opt: "+",
			},
			methods: {
				showInfo() {
					let value=0;
					switch (this.opt) {
						case '+':
							value = this.number1 + this.number2;
							break;
						case '-':
							value = this.number1 - this.number2;
							break;
						case '*':
							value = this.number1 * this.number2;
							break;
						case '/':
							value = this.number1 / this.number2;
							break;
					}
					this.val=this.number1+this.opt+this.number2+"="+value;
				}
				
			}
		})
	</script>
</html>

 2.用表达式的方式写:两个input一个option用v-model实现双向绑定,用v-if判断计算符号进行相对应的计算

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="js/vue.js"></script>
	</head>
	<body>
		<div id="app">
			<input type="text" v-model.number="number1" />
			<select v-model="opt">
				<option>+</option>
				<option>-</option>
				<option>*</option>
				<option>/</option>
			</select>
			<input type="text" v-model.number="number2" />
		<h2 v-if="opt=='+'">{{number1}}{{opt}}{{number2}}={{number1+number2}}</h2>
		<h2 v-if="opt=='-'">{{number1}}{{opt}}{{number2}}={{number1-number2}}</h2>
		<h2 v-if="opt=='*'">{{number1}}{{opt}}{{number2}}={{number1*number2}}</h2>
		<h2 v-if="opt=='/'">{{number1}}{{opt}}{{number2}}={{number1/number2}}</h2>
	</body>
	<script type="text/javascript">
		var vm = new Vue({
			el: "#app",
			data: {
				number1: 0,
				number2: 0,
				opt: "+",
			}
		})
	</script>
</html>

3.用监听器的方法写:两个input一个option用v-model实现双向绑定,用watch监听每一个计算变量,当值发生改变时,用新改变的值进行相对应的计算。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script type="text/javascript" src="js/vue.js"></script>
	</head>
	<body>
		<div id="app">
			<input type="text" v-model.number="number1" />
			<select v-model="opt">
				<option>+</option>
				<option>-</option>
				<option>*</option>
				<option>/</option>
			</select>
			<input type="text" v-model.number="number2" />
			<h2>{{val}}</h2>
		</div>
	</body>
	<script type="text/javascript">
		var vm = new Vue({
			el: "#app",
			data: {
				number1: 0,
				number2: 0,
				opt: "+",
				val:"",
			},
			watch:{
				number1(xin){
					let value=0;
					switch (this.opt) {
						case '+':
							value = xin + this.number2;
							break;
						case '-':
							value = xin - this.number2;
							break;
						case '*':
							value = xin * this.number2;
							break;
						case '/':
							value = xin / this.number2;
							break;
					}
					this.val=xin+this.opt+this.number2+"="+value;
				},
				number2(xin){
					let value=0;
					switch (this.opt) {
						case '+':
							value = this.number1 + xin;
							break;
						case '-':
							value = this.number1 - xin;
							break;
						case '*':
							value = this.number1 * xin;
							break;
						case '/':
							value = this.number1 / xin;
							break;
					}
					this.val=this.number1+this.opt+xin+"="+value;
				},
				opt(xin){
					let value=0;
					switch (xin) {
						case '+':
							value = this.number1 + this.number2;
							break;
						case '-':
							value = this.number1 - this.number2;
							break;
						case '*':
							value = this.number1 * this.number2;
							break;
						case '/':
							value = this.number1 / this.number2;
							break;
					}
					this.val=this.number1+this.opt+this.number2+"="+value;
				}
			}
			
		})
	</script>
</html>

4.用计算属性的方式写:两个input一个option用v-model实现双向绑定,用computed定义一个计算属性number3,当number3里面变量的值发生改变时,number3会重新计算。

<!DOCTYPE html>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script type="text/javascript" src="js/vue.js"></script>
	</head>
	<body>
		<div id="app">
			<input type="text" v-model.number="number1" />
			<select v-model="opt">
				<option>+</option>
				<option>-</option>
				<option>*</option>
				<option>/</option>
			</select>
			<input type="text" v-model.number="number2" />
			<h2>{{number3}}</h2>
		</div>
	</body>
	<script type="text/javascript">
		var vm = new Vue({
			el: "#app",
			data: {
				number1: 0,
				number2: 0,
				opt: "+",
			},
			computed: {
				number3(){
					let value = 0;
					switch (this.opt) {
						case '+':
							value = this.number1 + this.number2;
							break;
						case '-':
							value = this.number1 - this.number2;
							break;
						case '*':
							value = this.number1 * this.number2;
							break;
						case '/':
							value = this.number1 / this.number2;
							break;
					}
					return this.number1 + this.opt + this.number2 + "=" + value;
				}
			}
		})
	</script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新青年楷模

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值