使用vue,计算属性等完成计算器

引入vue文件

e9df63f7dffb4d5290d774c71230d4b0.png

 

先用v-model指令将输入框与数据属性绑定在一起,再用双花括号语法将其显示出来。

b1b81f49c7284af6a84d7648458bdfe5.png

创建一个 Vue 实例,定义数据属性,并重新定义选择数据计算属性。

c9dae9a3b4db4fe8935c78ec1b9a131b.png

 0c7c86df85db48169998d50f64c49dcf.png

 效果图如下:58e6065acd904dd1b74cf0bc9fa88082.png


完整代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3.  
  4.  <head>
  5.   <meta charset="utf-8" />
  6.   <title></title>
  7.   <script type="text/javascript" src="js/vue.js"></script>
  8.  </head>
  9.  
  10.  <body>
  11.   <div id="app">
  12.    <input type="text" placeholder="输入第一个数" v-model.number="num1">
  13.    <select v-model="sign">
  14.     <option>+</option>
  15.     <option>-</option>
  16.     <option>*</option>
  17.     <option>/</option>
  18.    </select>
  19.    <input type="text" placeholder="输入第二个数" v-model.number="num2"> <br> 答案:{{res}} <br>
  20.    <input type="text" placeholder="输入第一个数" v-model.number="num3">
  21.    <select v-model="sign1">
  22.     <option>+</option>
  23.     <option>-</option>
  24.     <option>*</option>
  25.     <option>/</option>
  26.    </select>
  27.    <input type="text" placeholder="输入第二个数" v-model.number="num4"> <br> 答案:{{res1}} <br>
  28.    <input type="text" placeholder="输入第一个数" v-model.number="num5" @change="change">
  29.    <select v-model="sign2" @change="change">
  30.     <option>+</option>
  31.     <option>-</option>
  32.     <option>*</option>
  33.     <option>/</option>
  34.    </select>
  35.    <input type="text" placeholder="输入第二个数" v-model.number="num6" @change="change"> <br> 答案:{{res2}}
  36.   </div>
  37.   <script>
  38.    const vm = new Vue({
  39.     el: "#app",
  40.     data: {
  41.      num1: 0,
  42.      num2: 0,
  43.      sign: "+",
  44.      num3: 0,
  45.      num4: 0,
  46.      sign1: "+",
  47.      res1: "",
  48.      num5: 0,
  49.      num6: 0,
  50.      sign2: "+",
  51.      res2: ""
  52.     },
  53.     methods: {
  54.      change() {
  55.       if(this.sign2 === "+") {
  56.        this.res2 = this.num5 + this.num6;
  57.       } else if(this.sign2 === "-") {
  58.        this.res2 = this.num5 - this.num6;
  59.       } else if(this.sign2 === "*") {
  60.        this.res2 = this.num5 * this.num6;
  61.       } else if(this.sign2 === "/") {
  62.        this.res2 = this.num5 / this.num6;
  63.       }
  64.      }
  65.     },
  66.         computed: {
  67.      res() {
  68.       if(this.sign === "+") {
  69.        return this.num1 + this.num2;
  70.       } else if(this.sign === "-") {
  71.        return this.num1 - this.num2;
  72.       } else if(this.sign === "*") {
  73.        return this.num1 * this.num2;
  74.       } else if(this.sign === "/") {
  75.        return this.num1 / this.num2;
  76.       }
  77.      }
  78.     },
  79.     watch: {
  80.      num3(val) {
  81.       if(this.sign1 === "+") {
  82.        this.res1 = val + this.num4;
  83.       } else if(this.sign1 === "-") {
  84.        this.res1 = val - this.num4;
  85.       } else if(this.sign1 === "*") {
  86.        this.res1 = val * this.num4;
  87.       } else if(this.sign1 === "/") {
  88.        this.res1 = val / this.num4;
  89.       }
  90.      },
  91.      num4(val) {
  92.       if(this.sign1 === "+") {
  93.        this.res1 = this.num3 + val;
  94.       } else if(this.sign1 === "-") {
  95.        this.res1 = this.num3 - val;
  96.       } else if(this.sign1 === "*") {
  97.        this.res1 = this.num3 * val;
  98.       } else if(this.sign1 === "/") {
  99.        this.res1 = this.num3 / val;
  100.       }
  101.      },
  102.      sign1(val) {
  103.       if(val === "+") {
  104.        this.res1 = this.num3 + this.num4;
  105.       } else if(val === "-") {
  106.        this.res1 = this.num3 - this.num4;
  107.       } else if(val === "*") {
  108.        this.res1 = this.num3 * this.num4;
  109.       } else if(val === "/") {
  110.        this.res1 = this.num3 / this.num4;
  111.       }
  112.      }
  113.     }
  114.    })
  115.   </script>
  116.  </body>
  117. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值