时间:2021-03-20 09:59:52 | 栏目:vue | 点击:次
本文实例为大家分享了Vue实现简易计算器的具体代码,供大家参考,具体内容如下
<html> <body> <div id="demo"> <h2>简单加减乘除计算器</h2> <p>请输入第一个数:<input type="text" v-model="firNum"></p> <p>请输入第二个数:<input type="text" v-model="secNum"></p> <p> <button @click="plus">+</button> <button @click="dec">-</button> <button @click="multiple">x</button> <button @click="devise">/</button> </p> <p>计算结果:<span v-text="result"></span></p> </div> <script> var v = new Vue({ el:"#demo", data:{ firNum:"", secNum:"", result:"" }, methods:{ plus:function () { // this.result = this.firNum + this.second; //这样只是字符拼接 this.result = parseInt(this.firNum) + parseInt(this.secNum); }, dec:function () { this.result = parseInt(this.firNum) - parseInt(this.secNum); }, multiple:function () { this.result = parseInt(this.firNum) * parseInt(this.secNum); }, devise:function () { this.result = parseInt(this.firNum) / parseInt(this.secNum); } } }) </script> </body> </html>
效果: