分别使用方法、监听属性、计算属性来实现计算器功能

1.实例化vue对象

 <script>
            var vm = new Vue( {
                el: '#app',
                data: {
                    //输入的第一个数字
                    num1: '',
                    //输入的第二个数字
                    num2: '',
                    //运算符
                    opt: '+',
                    //方法计算结果
                    results: 0,
                    //监听属性计算结果
                    result: 0
 
                },

2.用input接收用户输入的值,利用v-model对数据进行绑定

 <body>
        <div id="app">
            <input type="text" placeholder="请输入第一个数字" v-model.number="num1">
            <select v-model="opt">
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
            <input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
            <button @click="change">计算</button>
            <br>方法结果:{{results}}
            <br>计算属性结果:{{changes}}
            <br>监听属性结果:{{result}}

3.使用方法实现计算器功能

<!DOCTYPE html>
<html>
 
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title></title>
        <script src="./js/vue.js"></script>
    </head>
 
    <body>
        <div id="app">
            <input type="text" placeholder="请输入第一个数字" v-model.number="num1">
            <select v-model="opt">
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
            <input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
            <button @click="change">计算</button>
            <br>方法结果:{{results}}
            <br>计算属性结果:{{changes}}
            <br>监听属性结果:{{result}}
        </div>
        <script>
            var vm = new Vue( {
                el: '#app',
                data: {
                    //输入的第一个数字
                    num1: '',
                    //输入的第二个数字
                    num2: '',
                    //运算符
                    opt: '+',
                    //方法计算结果
                    results: 0,
                    //监听属性计算结果
                    result: 0
 
                },
                //方法实现计算器功能
                methods: {
                    change ()
                    {
                        if ( this.opt == '+' )
                        {
                            this.results = this.num1 + this.num2;
                        } else if ( this.opt == '-' )
                        {
                            this.results = this.num1 - this.num2;
                        } else if ( this.opt == '*' )
                        {
                            this.results = this.num1 * this.num2;
                        } else if ( this.opt == '/' )
                        {
                            this.results = this.num1 / this.num2;
                        }
                    }

4.使用监听属性实现计算器功能

<!DOCTYPE html>
<html>
 
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title></title>
        <script src="./js/vue.js"></script>
    </head>
 
    <body>
        <div id="app">
            <input type="text" placeholder="请输入第一个数字" v-model.number="num1">
            <select v-model="opt">
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
            <input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
            <button @click="change">计算</button>
            <br>方法结果:{{results}}
            <br>计算属性结果:{{changes}}
            <br>监听属性结果:{{result}}
        </div>
        <script>
            var vm = new Vue( {
                el: '#app',
                data: {
                    //输入的第一个数字
                    num1: '',
                    //输入的第二个数字
                    num2: '',
                    //运算符
                    opt: '+',
                    //方法计算结果
                    results: 0,
                    //监听属性计算结果
                    result: 0
 
                },
              
                //监听属性实现计算器功能
                watch: {
                    opt:function ( val )
                    {
                        if ( val == '+' )
                        {
                            this.result = this.num1 + this.num2;
                        } else if ( val == '-' )
                        {
                            this.result = this.num1 - this.num2;
                        } else if ( val == '*' )
                        {
                            this.result = this.num1 * this.num2;
                        } else if ( val == '/' )
                        {
                            this.result = this.num1 / this.num2;
                        }
                    },
                    num1:function ( val )
                    {
                        if ( this.opt == '+' )
                        {
                            return this.result = val + this.num2;
                        } else if ( this.opt == '-' )
                        {
                            return this.result = val - this.num2;
                        } else if ( this.opt == '*' )
                        {
                            return this.result = val * this.num2;
                        } else if ( this.opt == '/' )
                        {
                            return this.result = val / this.num2;
                        }
                    },
                    num2:function ( val )
                    {
                        if ( this.opt == '+' )
                        {
                            this.result = this.num1 + val;
                        } else if ( this.opt == '-' )
                        {
                            this.result = this.num1 - val;
                        } else if ( this.opt == '*' )
                        {
                            this.result = this.num1 * val;
                        } else if ( this.opt == '/' )
                        {
                            this.result = this.num1 / val;
                        }
                    }
                }
            } )
        </script>
    </body>
 
</html>

5.使用计算属性实现计算器功能

 <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title></title>
        <script src="./js/vue.js"></script>
    </head>
 
    <body>
        <div id="app">
            <input type="text" placeholder="请输入第一个数字" v-model.number="num1">
            <select v-model="opt">
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
            <input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
            <button @click="change">计算</button>
            <br>方法结果:{{results}}
            <br>计算属性结果:{{changes}}
            <br>监听属性结果:{{result}}
        </div>
        <script>
            var vm = new Vue( {
                el: '#app',
                data: {
                    //输入的第一个数字
                    num1: '',
                    //输入的第二个数字
                    num2: '',
                    //运算符
                    opt: '+',
                    //方法计算结果
                    results: 0,
                    //监听属性计算结果
                    result: 0
 
                },
              
                //计算属性实现计算器功能
                computed: {
                    changes ()
                    {
                        if ( this.opt == '+' )
                        {
                            return this.result = this.num1 + this.num2;
                        } else if ( this.opt == '-' )
                        {
                            return this.result = this.num1 - this.num2;
                        } else if ( this.opt == '*' )
                        {
                            return this.result = this.num1 * this.num2;
                        } else if ( this.opt == '/' )
                        {
                            return this.result = this.num1 / this.num2;
                        }
                    }
                },
               
        </script>

6.完整代码如下

<!DOCTYPE html>
<html>
 
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title></title>
        <script src="./js/vue.js"></script>
    </head>
 
    <body>
        <div id="app">
            <input type="text" placeholder="请输入第一个数字" v-model.number="num1">
            <select v-model="opt">
                <option value="+">+</option>
                <option value="-">-</option>
                <option value="*">*</option>
                <option value="/">/</option>
            </select>
            <input type="text" placeholder="请输入第二个数字" v-model.number="num2"><br>
            <button @click="change">计算</button>
            <br>方法结果:{{results}}
            <br>计算属性结果:{{changes}}
            <br>监听属性结果:{{result}}
        </div>
        <script>
            var vm = new Vue( {
                el: '#app',
                data: {
                    //输入的第一个数字
                    num1: '',
                    //输入的第二个数字
                    num2: '',
                    //运算符
                    opt: '+',
                    //方法计算结果
                    results: 0,
                    //监听属性计算结果
                    result: 0
 
                },
                //方法实现计算器功能
                methods: {
                    change ()
                    {
                        if ( this.opt == '+' )
                        {
                            this.results = this.num1 + this.num2;
                        } else if ( this.opt == '-' )
                        {
                            this.results = this.num1 - this.num2;
                        } else if ( this.opt == '*' )
                        {
                            this.results = this.num1 * this.num2;
                        } else if ( this.opt == '/' )
                        {
                            this.results = this.num1 / this.num2;
                        }
                    }
                },
                //计算属性实现计算器功能
                computed: {
                    changes ()
                    {
                        if ( this.opt == '+' )
                        {
                            return this.result = this.num1 + this.num2;
                        } else if ( this.opt == '-' )
                        {
                            return this.result = this.num1 - this.num2;
                        } else if ( this.opt == '*' )
                        {
                            return this.result = this.num1 * this.num2;
                        } else if ( this.opt == '/' )
                        {
                            return this.result = this.num1 / this.num2;
                        }
                    }
                },
                //监听属性实现计算器功能
                watch: {
                    opt:function ( val )
                    {
                        if ( val == '+' )
                        {
                            this.result = this.num1 + this.num2;
                        } else if ( val == '-' )
                        {
                            this.result = this.num1 - this.num2;
                        } else if ( val == '*' )
                        {
                            this.result = this.num1 * this.num2;
                        } else if ( val == '/' )
                        {
                            this.result = this.num1 / this.num2;
                        }
                    },
                    num1:function ( val )
                    {
                        if ( this.opt == '+' )
                        {
                            return this.result = val + this.num2;
                        } else if ( this.opt == '-' )
                        {
                            return this.result = val - this.num2;
                        } else if ( this.opt == '*' )
                        {
                            return this.result = val * this.num2;
                        } else if ( this.opt == '/' )
                        {
                            return this.result = val / this.num2;
                        }
                    },
                    num2:function ( val )
                    {
                        if ( this.opt == '+' )
                        {
                            this.result = this.num1 + val;
                        } else if ( this.opt == '-' )
                        {
                            this.result = this.num1 - val;
                        } else if ( this.opt == '*' )
                        {
                            this.result = this.num1 * val;
                        } else if ( this.opt == '/' )
                        {
                            this.result = this.num1 / val;
                        }
                    }
                }
            } )
        </script>
    </body>
 
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值