Vue implements simple calculator function

Vue implements simple calculator function

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

Function Introduction

1. Addition, subtraction, multiplication and division can be realized
2. Can achieve zeroing
3. Implement backspace

The renderings are average, the style is random, mainly focusing on the function and implementation method

Code and explanation

1. HTML part

First, lay out the layout, write out what you want to do, and bind a click event to each button

<div id="box">
  <table>
    <tr>
      <td><input type="button"value="del" @click="del()"></td>
      <td><input type="button"value="C" @click="clean()"></td>
      <td colspan="2"><input type="text" style="width: 200px" value="" v-model="rel"></td>

    </tr>
    <tr>
      <td><input type="button"value="7" @click="add('7')"></td>
      <td><input type="button"value="8" @click="add('8')"></td>
      <td><input type="button"value="9" @click="add('9')"></td>
      <td><input type="button"value="/" @click="add('/')"></td>
    </tr>


    <tr>
      <td><input type="button"value="4" @click="add('4')"></td>
      <td><input type="button"value="5" @click="add('5')"></td>
      <td><input type="button"value="6" @click="add('6')"></td>
      <td><input type="button"value="*" @click="add('*')"></td>
    </tr>

    <tr>
      <td><input type="button"value="1" @click="add('1')"></td>
      <td><input type="button"value="2" @click="add('2')"></td>
      <td><input type="button"value="3" @click="add('3')"></td>
      <td><input type="button"value="-" @click="add('-')"></td>
    </tr>

    <tr>
      <td><input type="button"value="0" @click="add('0')"></td>
      <td><input type="button"value="." @click="add('.')"></td>
      <td><input type="button"value="+" @click="add('+')"></td>
      <td><input type="button" value="=" v-on:click="result()" ></td>
    </tr>


  </table>
</div>

2. CSS part, just write the style casually, it is not very important

input{
      width: 100px;
      height: 100px;
      border: 1px solid black;
      line-height: 100px;
      text-align: center;
      border-radius: 10px;
      background-color: gainsboro;
      outline: none;
    }
    table{
      background-color: #b3d7ff;
      margin: auto;
}

3. Finally, the part about vm instance

var vm = new Vue({
    el:"#box",
    data:{
      rel:"",

    },
    methods:{
       add(index){//Here is the key binding method, concatenate the obtained value to the rel string this.rel +=index;
      },
      result(){
        this.rel = eval(this.rel);//Here is a calculation using the eval method this.rel = String(this.rel);//The purpose here is to display the number or string in the display bar. Only strings can be backspaced and reset to zero},
      del(){//This is the backspace operation. Use the substring method of the string to intercept. Each interception starts from the 0th one and goes to the one before the length, which is equivalent to backspace.
      this.rel = this.rel.substring(0,this.rel.length-1);
      },
      clean(){//This is the method of zeroing. The zeroing operation is achieved by assigning an empty string to the result. Of course, you can also use the deletion method, such as the unshift method or the pop method. It is easier to directly assign a value of empty.
         this.rel = "";
      }
    }
})

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
  • Vue.js implements price calculator function
  • Example of classic calculator/scientific calculator function implemented by vue.js
  • Implementing a simple calculator using Vue
  • Vue implements calculator function
  • Vue.js implements simple calculator function
  • Vue implements a simple calculator
  • Vue.js implements a three-dimensional calculator
  • Vue implements mobile calculator
  • Vue implements a simple addition calculator

<<:  Use PSSH to batch manage Linux servers

>>:  MySQL 5.6.28 installation and configuration tutorial under Linux (Ubuntu)

Recommend

How to implement HTML Table blank cell completion

When I first taught myself web development, there...

Sharing of two website page translation plug-ins

TranslateThis URL: http://translateth.is Google T...

WeChat applet implements search function and jumps to search results page

Search Page: search.wxml page: <view class=&qu...

jQuery plugin to implement search history

A jQuery plugin every day - to make search histor...

Sharing ideas on processing tens of millions of data in a single MySQL table

Table of contents Project Background Improvement ...

Vue commonly used high-order functions and comprehensive examples

1. Commonly used high-order functions of arrays S...

js Promise concurrent control method

Table of contents question background Idea & ...

Correct use of MySQL partition tables

Overview of MySQL Partitioned Tables We often enc...

How to modify the master-slave replication options in MySQL online

Preface: The most commonly used architecture of M...

CSS implements the web component function of sliding the message panel

Hello everyone, I wonder if you have the same con...

Specific method to add foreign key constraints in mysql

The operating environment of this tutorial: Windo...

Some ways to solve the problem of Jenkins integrated docker plugin

Table of contents background Question 1 Error 2 E...

Front-end JavaScript Promise

Table of contents 1. What is Promise 2. Basic usa...

my.cnf parameter configuration to optimize InnoDB engine performance

I have read countless my.cnf configurations on th...