Vue编写简易计算器

 时间:2026-02-18 07:36:05

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中如何为页面添加背景图片?
  • 热门搜索
    趣味数学手抄报 消防安全的手抄报 美丽家乡手抄报 祖国我为你骄傲手抄报 禁毒手抄报的内容 名言警句手抄报 元旦手抄报资料 手抄报素材 一年级数学手抄报 交通安全漫画手抄报