vue中实现计算器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="vue.js"></script>
</head>
<body>
    <!--容器-->
    <div id="app">
        <input type="text" v-model="n1">
        <select v-model="type">
            <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="result">
    </div>

<script>
    // 创建一个vue实例
    var vm=new Vue({
        el:'#app',  // 绑定id为appid容器
        data:{
            n1:0,
            n2:0,
            type:'-',
            result:''
        },
        methods:{
            cal(){
                console.log("adsf");
                switch (this.type){
                    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>
</html>

效果:

猜你喜欢

转载自blog.csdn.net/qq_31293575/article/details/81120374