Vue implements a simple calculator

Vue implements a simple calculator

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

Case Requirements

Case ideas

1. Binding of values ​​A and B is realized through v-model directive
2. Bind events to the calculation buttons to implement calculation logic
3. Bind the calculation results to the corresponding positions

Implementing static pages

<div id='app'>
    <h1>Simple Calculator</h1>
    <div><span>Value A:</span><span><input type="text" v-model='a'></span></div>
    <div><span>Value B:</span><span type="text" v-model='b'></span></div>
    <div><button>Calculate</button></div>
    <div><span>Calculation results</span><span></span></div>
</div>

Importing Vue

<script type="text/javascript" src="js/vue.js"></script>

Add instructions for static pages

<div id='app'>
    <h1>Simple Calculator</h1>
    <div><span>Value A:</span>
      <span>
        <input type="text" v-model='a'>
      </span>
    </div>
    <div>
      <span>Value B:</span>
      <span>
        <input type="text" v-model='b'>
      </span>
    </div>
    <div>
      <button v-on:click="handle">Calculate</button>
    </div>
    <div><span>Calculation result</span><span v-text="result"></span></div>
</div>

Set calculation function

<script type="text/javascript">
    /* */
    var vm = new Vue({
      el: "#app",
      data: {
        a: '',
        b: '',
        result: ''
      },
      methods: {
        handle: function () {
          // Implement calculation logic this.result = parseInt(this.a) + parseInt(this.b);
        }
      }
    });
</script>

Final code

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Simple Calculator</title>
</head>

<body>
  <div id='app'>
    <h1>Simple Calculator</h1>
    <div><span>Value A:</span>
      <span>
        <input type="text" v-model='a'>
      </span>
    </div>
    <div>
      <span>Value B:</span>
      <span>
        <input type="text" v-model='b'>
      </span>
    </div>
    <div>
      <button v-on:click="handle">Calculate</button>
    </div>
    <div><span>Calculation result</span><span v-text="result"></span></div>
  </div>
  <script type="text/javascript" src="js/vue.js"></script>
  <script type="text/javascript">
    /* */
    var vm = new Vue({
      el: "#app",
      data: {
        a: '',
        b: '',
        result: ''
      },
      methods: {
        handle: function () {
          // Implement calculation logic this.result = parseInt(this.a) + parseInt(this.b);
        }
      }
    });
  </script>
</body>

</html>

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 implements a simple addition calculator
  • Vue.js implements price calculator function
  • Vue implements calculator function
  • Vue implements a simple calculator
  • Vue implements mobile calculator
  • Vue.js implements simple calculator function
  • Vue implements a simple web calculator

<<:  SQL GROUP BY detailed explanation and simple example

>>:  Detailed explanation of Linux file operation knowledge points

Recommend

The practical process of login status management in the vuex project

Table of contents tool: Login scenario: practice:...

How to deploy Node.js with Docker

Preface Node will be used as the middle layer in ...

Detailed explanation of uniapp's global variable implementation

Preface This article summarizes some implementati...

Analysis of two implementation methods for adding static routing in Linux

Command to add a route: 1.Route add route add -ne...

MySQL NULL data conversion method (must read)

When using MySQL to query the database and execut...

Let's talk in detail about the difference between unknown and any in TypeScript

Table of contents Preface 1. unknown vs any 2. Th...

Detailed process of Vue front-end packaging

Table of contents 1. Add packaging command 2. Run...

This article will help you understand the life cycle in Vue

Table of contents 1. beforeCreate & created 2...

How to use JavaScript strategy pattern to validate forms

Table of contents Overview Form validation withou...

Teach you step by step to develop a brick-breaking game with vue3

Preface I wrote a few examples using vue3, and I ...

Write a simple calculator using JavaScript

The effect is as follows:Reference Program: <!...

HTML form component example code

HTML forms are used to collect different types of...

Detailed explanation on how to modify the default port of nginx

First find out where the configuration file is wh...