v-model 练习,简易计算器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <input type="text" name="" id="" v-model:value="n1">
        <select name="" id="" v-model:value="opt">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        <input type="text" v-model:value="n2">
        <input type="button" value="=" @click="calc">
        <input type="text" v-model="result"> 
    </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
     var app= new Vue({
        el:'#app',
        data:{
            n1:0,
            n2:0,
            result:0,
            opt:'+'

        },
        methods:{
            calc(){
                // var codeStr='prseInt(this.n1)'+this.opt+'prseInt(this.n2)'
                // this.result=eval(codeStr)
                
                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>
</html>
posted on 2021-09-10 11:12  果冻TD  阅读(49)  评论(0编辑  收藏  举报