首页 > 其他 > 详细

Vue_使用v-model指令写的简易计算器

时间:2020-03-17 13:19:59      阅读:93      评论:0      收藏:0      [点我收藏+]

v-model指令对数据进行双向绑定

<body>
    <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" @click="calc" value="=">
        <input type="text" v-model="result">
    </div>
    <script>
        var vm = new Vue({
            el: "#app",
            data: {
                n1: 1,
                n2: 1,
                result: 2,
                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>
</body>
  • 或者用eval()函数偷个懒
var strCode='parseInt(this.n1)'+this.opt+'parseInt(this.n2)';
this.result=eval(strCode);

eval()函数能不用就不用

Vue_使用v-model指令写的简易计算器

原文:https://www.cnblogs.com/Syinho/p/12354624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!