03-vue案例:计数器的使用

一、直接使用conter函数增加,减少来使用

<!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>03-vue案例-计数器</title>
    <script src="../js/vue.js"></script>
</head>
<body>
     <div id="app">
         <h1>当前计数:{{couter}}</h1>
         <button v-on:click="couter++">+</button>
         <button v-on:click="couter--">-</button>
         <!--可以语法糖v-on:click也可以使用@click来实现 //直接通过button中的参数couter来实现计数的增加 -->
         <!-- 可以通过函数来实现 -->   
         <!-- <button v-on:click="add">+</button>  
         <button v-on:click="sub">-</button> -->
     </div>
    <script>
        const app=new Vue({
        el:'#app',
        data:{
            couter:0
        },
        // method:{
        //     add:function(){
        //      this.couter++,
        //      console.log("add函数被使用!")
        //     },
        //     sub:function(){
        //         this.couter--,
        //         console .log('sub函数被调用!')
        //     }
        // }
        })  
    </script>
    
</body>
</html>

 

运行结果:

 

 

 

二、通过add()和sub()函数来定义使用

<!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>03-vue案例-计数器</title>
    <script src="../js/vue.js"></script>
</head>
<body>
     <div id="app">
         <h1>当前计数:{{couter}}</h1>
         <!-- <button v-on:click="couter++">+</button>
         <button v-on:click="couter--">-</button> -->
         <!-- //直接通过button中的参数couter来实现计数的增加 -->
         <!-- 可以通过函数来实现 -->   
         <button v-on:click="add">+</button>  
         <button v-on:click="sub">-</button>
     </div>
    <script>
        const app=new Vue({
        el:'#app',
        data:{
            couter:0
        },
         methods:{
            add:function(){
             this.couter++,
             console.log("add函数被执行!")
            },
            sub:function(){
                this.couter--,
                console.log('sub函数被执行!')
            }
        }
        })  
    </script>
    
</body>
</html>

 

 两种计数器都可以实现了!每天进步一点点,总会有好的结果!

 

posted @ 2022-02-13 22:28  starter123  阅读(183)  评论(0)    收藏  举报