Vue编写简易计算器

 时间:2026-05-05 08:26:57

1、引入vue.js

<head>


   <meta charset="UTF-8">
   <title>Vue简易计算器</title>
   <script src="../lib/vue.js"></script>
</head>

2、编写基本HTML结构

<div id="app">


   <input type="text" v-model="n1">
   <select name="" id="" 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="=" v-on:click="calc">
   <input type="text" v-model="result">
</div>

3、vue绑定对象

<script>


   var app = new Vue({
       el: '#app',
       data: {
           n1: '',
           n2: '',
           result: '',
           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;
               }  
           }
       }
   })
</script>

4、效果显示

Vue编写简易计算器

  • intellij idea如何快速生成类的构造方法
  • IDEA界面如何设为中文?
  • netbeans怎么修改字体和字体大小
  • phpstudy无法启动mysql服务器怎么办?
  • Android Studio中如何为页面添加背景图片?
  • 热门搜索
    怎么装修房子 颤音怎么练 输入法不见了怎么恢复 下眼皮跳是怎么回事 怎么吹头发 笔记本电脑开机黑屏怎么办 怎么选车 婴儿流鼻涕怎么办 手麻脚麻是怎么回事 怎么打乒乓球