Vue实现简易计算器

简介: Vue实现简易计算器

简易计算器案例



1. HTML 代码结构


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


2. Vue实例代码:


  // 创建 Vue 实例,得到 ViewModel
    var vm = new Vue({
      el: '#app',
      data: {
        n1: 0,
        n2: 0,
        result: 0,
        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;
          } 
          // 注意:这是投机取巧的方式,正式开发中,尽量少用
          // var codeStr = 'parseInt(this.n1) ' + this.opt + ' parseInt(this.n2)'
          // this.result = eval(codeStr)
        }
      }
    });


3. 全部代码


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./lib/vue-2.4.0.js"></script>
</head>
<body>
  <div id="app">
    <input type="text" v-model="n1">
    <select 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="=" @click="calc">
    <input type="text" v-model="result">
  </div>
  <script>
    // 创建 Vue 实例,得到 ViewModel
    var vm = new Vue({
      el: '#app',
      data: {
        n1: 0,
        n2: 0,
        result: 0,
        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)
              breakvalue_name: value,            case '/':
              this.result = parseInt(this.n1) / parseInt(this.n2)
              break;
          } 
          // 注意:这是投机取巧的方式,正式开发中,尽量少用
          // var codeStr = 'parseInt(this.n1) ' + this.opt + ' parseInt(this.n2)'
          // this.result = eval(codeStr)
        }
      }
    });
  </script>
</body>
</html> 
相关文章
|
1天前
|
存储 JavaScript
Vue当前时间与接口返回时间的判断
Vue当前时间与接口返回时间的判断
7 0
|
1天前
|
JavaScript
vue生成动态表单
vue生成动态表单
6 0
|
1天前
|
JavaScript 前端开发
Vue生成Canvas二维码
Vue生成Canvas二维码
6 0
|
1天前
|
JavaScript
vue项目切换页面白屏的解决方案
vue项目切换页面白屏的解决方案
5 0
|
1天前
|
JavaScript 前端开发 开发者
new Vue() 发生了什么
new Vue() 发生了什么
8 1
|
1天前
|
JavaScript 容器
使用Vue写一个日期选择器
使用Vue写一个日期选择器
9 1
|
1天前
|
JavaScript
Vue 中如何模块化使用 Vuex
Vue 中如何模块化使用 Vuex
5 0
|
1天前
|
JavaScript 应用服务中间件 nginx
vue中404解决方法
vue中404解决方法
3 0
|
1天前
|
JavaScript 前端开发
vue中nextTick使用以及原理
vue中nextTick使用以及原理
5 0
|
1天前
|
JavaScript
vue实现多个el-form表单提交统一校验的2个方法
vue实现多个el-form表单提交统一校验的2个方法
5 0