Vue implements simple calculator function

Vue implements simple calculator function

This article example shares the specific code of Vue to realize the simple calculator function for your reference. The specific content is as follows

Function: Add, subtract, multiply and divide the values ​​in the two input boxes

Knowledge points used:

1. v-model data two-way binding

2. .number is converted to a number

3.@click click event

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
    //Choose your own vue location <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script>
 </head>
 <body>
  <div id="box">
   <!-- Convert the first value to a number -->
   <input type="number" v-model.number="num1"/>
   
   <select v-model="str">
    <option value="+">+</option>
    <option value="-">-</option>
    <option value="*">*</option>
    <option value="/">/</option>
   </select>
   <input type="number" v-model.number="num2"/>
   <!-- Click the equal sign to execute the contents of the cal function-->
   <button type="button" @click="calc">=</button>
   <!-- Output the result -->
   <input type="text"/ v-model="num3">
  </div>
 </body>
 <script type="text/javascript">
  var vm = new Vue({
   el:"#box",
   data:{
    // The initial value in the input box num1: 0,
     num2:0,
     num3:0,
     str:'+'
    
   },
   methods:{
    calc(){
     if(this.str=="+"){
      this.num3=this.num1+this.num2
     }else if(this.str=="-"){
      this.num3=this.num1-this.num2
     }else if(this.str=="*"){
      this.num3=this.num1*this.num2
     }else if(this.str=="/"){
      this.num3=this.num1/this.num2
     }
    }
   
   }
   
  })
 </script>
</html>

result:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Complete example of calculator function implemented by Vue.js
  • Implementing a simple calculator using Vue
  • Example of classic calculator/scientific calculator function implemented by vue.js
  • Vue.js implements price calculator function
  • Vue implements a simple addition calculator
  • Vue implements calculator function
  • Vue implements a simple calculator
  • Vue.js implements simple calculator function
  • Vue implements mobile calculator
  • Vue.js implements a three-dimensional calculator

<<:  Implementation idea of ​​left alignment of the last row of flex box layout

>>:  hr horizontal line style example code

Recommend

Vue installation and use

Table of contents 1. Vue installation Method 1: C...

Detailed explanation of nginx's default_server definition and matching rules

The default_server directive of nginx can define ...

Markup language - simplified tags

Click here to return to the 123WORDPRESS.COM HTML ...

Detailed explanation of when javascript scripts will be executed

JavaScript scripts can be embedded anywhere in HT...

How to solve the problem of blurry small icons on mobile devices

Preface Previously, I talked about the problem of...

Implementation of docker view container log command

Why should we read the log? For example, if the c...

A brief discussion on the types of node.js middleware

Table of contents Overview 1. Application-level m...

How to use Vuex's auxiliary functions

Table of contents mapState mapGetters mapMutation...

React new version life cycle hook function and usage detailed explanation

Compared with the old life cycle Three hooks are ...

Semantics, writing, and best practices of link A

The semantics, writing style, and best practices ...

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 >...

Vue folding display multi-line text component implementation code

Folding display multi-line text component Fold an...