1、引入vue.js<head> <meta charset="UTF-8"> <title>Vue简易计算器</title> <script src="../lib/vue.js"></script></head>
2、编老揉孟阼写基本HTML结构<div id="app"> <input type租涫疼迟="text" v-model="n1"> <select name="" id="" v-model="opt"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="text" v-model="n2"> <input type="button" value="=" v-on:click="calc"> <input type="text" v-model="result"></div>
3、vue绑定对象<script> var app = new Vue({ el: 泌驾台佐39;#app', data: { n1: '', n2: '', result: '', opt: '+' }, methods: { calc() { //判断操作符 switch (this.opt) { case "+": this.result = parseInt(this.n1) + parseInt(this.n2); break; case "-": this.result = parseInt(this.n1) - parseInt(this.n2); break; case "*": this.result = parseInt(this.n1) * parseInt(this.n2); break; case "/": this.result = parseInt(this.n1) / parseInt(this.n2); break; } } } })</script>
4、效果显示