<!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>简易计算器</title> <script src="../lib/vue.js"></script> </head> <body> <div class="app"> <input type="text" v-model="firstNum"> <select name="" id="" v-model="symbol"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="text" v-model="secondNum"> <input type="button" value="=" @click="count"> <input type="text" v-model="finallyNum"> </div> <!-- 简易计算器基本html结构 --> <script> var vm = new Vue({ el:‘.app‘, data:{ firstNum:‘‘, secondNum:‘‘, finallyNum:‘‘, symbol:‘+‘ //v-model双向数据绑定初始化值 }, methods:{ count(){ if(this.symbol==‘+‘){ this.finallyNum=parseInt(this.firstNum)+parseInt(this.secondNum); } if(this.symbol==‘*‘){ this.finallyNum=parseInt(this.firstNum)*parseInt(this.secondNum); } if(this.symbol==‘-‘){ this.finallyNum=parseInt(this.firstNum)-parseInt(this.secondNum); } if(this.symbol==‘/‘){ this.finallyNum=parseInt(this.firstNum)/parseInt(this.secondNum); } } //定义计算方法 } }) </script> </body> </html>
原文:https://www.cnblogs.com/qiqisusu/p/11342228.html