欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue实现简易计算器

程序员文章站 2022-05-23 11:06:11
简易计算器
... ......
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>简易计算器</title>
    <script src="./lib/vue.js"></script>
</head>
<body>
<div id="app">
    <input type="text" v-model="num1">
    <select v-model="opt">
        <option value="+">+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
    </select>
    <input type="text" v-model="num2">
    <input type="button" value="=" @click="cal">
    <input type="text" v-model="result">
</div>
<script>
    var vm = new vue({
        el:"#app",
        data:{
            num1:0,
            num2:0,
            result:0,
            opt:"+"
        },
        methods:{
            cal(){
                switch (this.opt) {
                    case "+":
                        this.result = parseint(this.num1) + parseint(this.num2)
                        break;
                    case "-":
                        this.result = parseint(this.num1) - parseint(this.num2)
                        break;
                    case "*":
                        this.result = parseint(this.num1) * parseint(this.num2)
                        break;
                    case "/":
                        this.result = parseint(this.num1) / parseint(this.num2)
                        break;
                }
            }
        }
    })
</script>
</body>
</html>