4、方法

2019-03-10  本文已影响0人  youi_e050

今天学习methods属性, 这个名字是固定的,它是一个对象,用于存储各种方法。{{方法名()}}就可以调用相应的方法。

使用method方法

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <div id="app">
            <h2>{{greeting('afternoon')}}</h2>
            <h2>{{message}}</h2>                    
        </div>
        <script type="text/javascript">
            //实例化一个vue对象
            var app = new Vue({
                el: '#app',
                data:{
                    message:' hello Vue',               
                },
                methods:{
                    greeting:function(time){
                        return 'Good' + time+'!';
                    }
                }
            })
        </script>
    </body>
</html>

运行结果:


image.png

使用v-on指令调用方法

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <!-- 通过cdn引入vue.js -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <!-- vue-app根容器 -->
        <div id="app">
            <h2 v-if="show">{{name}}</h2>
            <button type="button" v-on:click="handClick">隐藏/显示</button>
            

        </div>
        <script type="text/javascript">
            //实例化一个vue对象
            var app = new Vue({
                el: '#app',
                data:{
                    name:'2019',
                    show:true
                },
                methods:{
                    handClick:function(){
                        // 把当前的show属性相反
                        this.show=!this.show;
                    }
                }
            })
        </script>
        
    </body>
</html>

关注/取消关注练习

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <!-- 通过cdn引入vue.js -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css"/>
        <style type="text/css">
            .follow{
                color:#DDD;
            }
            .cancle-follow{
                color:#008000;
            }
            .link{
                cursor:pointer;
            }
        </style>
    </head>
    <body>
        <!-- vue-app根容器 -->
        <div id="app">
            <h2>{{name}}</h2>
            <p class="follow link" v-show="followed" v-on:click="handfollow">
                <i class="icon-ok"></i>已关注</p>
            <p class="cancle-follow link" v-show="followed===false" 
             v-on:click="handfollow"><i class="icon-plus"></i>关注</p>
            

        </div>
        <script type="text/javascript">
            //实例化一个vue对象
            var app = new Vue({
                el: '#app',
                data:{
                    name:'简书作者',
                    followed:false
                    
                },
                methods:{
                    handfollow:function(followed){
                        this.followed=!this.followed;
                    }
                }
                
            })
        </script>
        
    </body>
</html>

年龄的增减练习

单击“长一岁”可以使得age的值加1,单击“减五岁”可以使得age的值减5

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <!-- 通过cdn引入vue.js -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    </head>
    <body>
        <!-- vue-app根容器 -->
        <div id="app">
            <h2>{{age}}</h2>
            <button type="button" v-on:click="add">加一岁</button>
            <button type="button" v-on:click="substrct(5)">减5岁</button>

        </div>
        <script type="text/javascript">
            //实例化一个vue对象
            var app = new Vue({
                el: '#app',
                data:{
                    age:30
                    
                },
                methods:{
                    add:function(){
                        this.age += 1;
                    },
                    substrct:function(num){
                        this.age -= num;
                    }
                }
            })
        </script>
        
    </body>
</html>

大体效果:


image.png

关注和加减的综合训练:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <!-- 通过cdn引入vue.js -->
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css" />
        <style type="text/css">
            

            .btn {
                color: red;
            }

            .link {
                cursor: pointer;
            }
            .zan{
                display:flex;
                
            }
            .btn{
                width:100px;
                height:35px;
                border-radius: 20px;
                background-color:#FFF;
                
                border:1px solid rgb(236, 97, 73);
                outline:none;
            }
        </style>
    </head>
    <body>
        <!-- vue-app根容器 -->
        <div id="app">
            <div class="zan">
                
                
                <button class="btn link" v-show="followed" 
                v-on:click="handfollow" v-on:click="handfollow"><i class="icon-heart"></i>喜欢 |{{age}} </button>
                
                <button class="btn link" v-show="followed===false" v-on:click="handfollow">
                    <i class="icon-heart-empty"></i>喜欢 {{age}} </button>
            
            </div>

        </div>
        <script type="text/javascript">
            //实例化一个vue对象
            var app = new Vue({
                el: '#app',
                data: {
                    age: 12,
                    followed: false

                },
                methods: {
                    handfollow: function(followed) {
                        this.followed = !this.followed;
                        if(this.followed==true){
                            this.age+=1;
                        }
                        else{
                            this.age-=1;
                        }
                    }
                    
                }

            })
        </script>

    </body>
</html>

大体效果


image.png

可以点击按钮来喜欢或取消喜欢,同时后面数字也会随之改变。

上一篇 下一篇

猜你喜欢

热点阅读