SoFunction
Updated on 2025-04-05

vue-model implements simple calculator

This article shares the specific code of the vue-model implementation simple calculator for your reference. The specific content is as follows

<!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>Vue</title>
 <script src="../lib/vue-2.4."></script>
</head>
<body>
 
 <div  >
  <!-- Number one -->
  <input type="text" v-model='n1' placeholder="0">
  <!-- Addition, subtraction, multiplication and division -->
  <select v-model='opt'>
   <option value="+"> + </option>
   <option value="-">-</option>
   <option value="*">*</option>
   <option value="/">/</option>
  </select>
  <!-- number2 -->
  <input type="text" v-model='n2' placeholder="0">
  <!-- equal sign -->
  <input type="button" value='=' >
  <!-- result -->
  <input type="text" v-model='result' placeholder="0">
  <!-- OK button -->
  <input type="button" value='result' @click='calc'>
  <!-- Return to zero -->
  <input type="button" value='Return to Zero' @click='zero'>


 </div>

 <script>
  var vm = new Vue({
   el: '#app', //Indicates that this instance of the current new controls the area on the page data: { //The data attribute stores the data to be used in el    n1: '',
    n2:'',
    result:'',
    opt: '+'
   },
   methods:{
    calc(){
     // switch(){
     //  case '+':
     //   = parseInt(this.n1) + parseInt(this.n2)
     //  break;
     //  case '-':
     //   = parseInt(this.n1) - parseInt(this.n2)
     //  break;
     //  case '*':
     //   = parseInt(this.n1) * parseInt(this.n2)
     //  break;
     //  case '/':
     //   = parseInt(this.n1) / parseInt(this.n2)
     //  break;
     // }

     // Abbreviation     var codeStr = 'parseInt(this.n1) '+  +' parseInt(this.n2)'
      = eval(codeStr)
    },
    zero(){
     this.n1 = '',
     this.n2 = '',
      = '',
      = '+'
    }

   }
  }) 
 </script>

</body>
</html>

For technical articles related to calculator, please click on the topic:JavaScript Calculator Function Summary

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.