企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
>实现效果: ![](https://box.kancloud.cn/887c4638d01da4487287abc3581d32e5_802x112.gif) >html代码 ``` <div id="app"> <input type="text" v-model="n1"> <select 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="=" @click="cal"> <input type="text" v-model="res"> </div> ``` >js代码 ``` var vm = new Vue({ el: '#app', data: { n1: 0, n2: 0, res: 0, opt: '+' }, methods: { cal() { switch (this.opt) { case "+": this.res = parseFloat(this.n1) + parseFloat(this.n2); break; case '-': this.res = parseFloat(this.n1) - parseFloat(this.n2); break; case '*': this.res = parseFloat(this.n1) * parseFloat(this.n2); break; case '/': this.res = parseFloat(this.n1) / parseFloat(this.n2); break; } //注意:以下是投机取巧方式,正式开发中尽量少用 // var codeStr = 'parseFloat(this.n1)' + this.opt + 'parseFloat(this.n2)'; // console.log(codeStr); // this.res = eval(codeStr); } } }) ```