Vue2.0 【第四季】第3节 实例事件

Vue2.0 【第四季】第3节 实例事件


第3节 实例事件

实例事件就是在构造器外部写一个调用构造器内部的方法。这样写的好处是可以通过这种写法在构造器外部调用构造器内部的数据。

我们还是写一个点击按钮,持续加1的例子。

一、$on 在构造器外部添加事件

app.$on('reduce',function(){
    console.log('执行了reduce()');
    this.num--;
});

$on接收两个参数,第一个参数是调用时的事件名称,第二个参数是一个匿名方法。

如果按钮在作用域外部,可以利用$emit来执行。

//外部调用内部事件
function reduce(){
    app.$emit('reduce');
}

全部代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>example03</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
    <body>
        <h1>example03</h1>
        <hr>
        <div id="app">
            {{num}}
            <p><button @click="add">add</button></p>
        </div>
        <p><button onclick="reduce()">reduce</button></p>

        <script type="text/javascript">
            var app = new Vue({
                el:'#app',
                data:{
                    num:1
                },
                methods:{
                    add:function(){
                        this.num++;
                    }
                }
            });

            app.$on("reduce",function(){
                console.log("执行了reduce方法");
                this.num--;
            })

            function reduce(){
                app.$emit('reduce');
            }
        </script>
    </body>
</html>

浏览器效果:

二、$once执行一次的事件

app.$once('reduceOnce',function(){
    console.log('只执行一次的方法');
    this.num--;

});

全部代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>example03</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
    <body>
        <h1>example03</h1>
        <hr>
        <div id="app">
            {{num}}
            <p><button @click="add">add</button></p>
        </div>
        <p><button onclick="reduce()">reduce</button></p>
        <p><button onclick="reduceOnce()">reduceOnce</button></p>

        <script type="text/javascript">
            var app = new Vue({
                el:'#app',
                data:{
                    num:1
                },
                methods:{
                    add:function(){
                        this.num++;
                    }
                }
            });

            app.$on("reduce",function(){
                console.log("执行了reduce方法");
                this.num--;
            })

            app.$once('reduceOnce',function(){
                console.log('只执行一次的方法');
                this.num--;

            });

            function reduce(){
                app.$emit('reduce');
            }

            function reduceOnce(){
                app.$emit('reduceOnce');
            }
        </script>
    </body>
</html>

浏览器效果:

三、$off关闭事件

···
//关闭事件
function off(){
app.$off('reduce');
}
···

全部代码:

扫描二维码关注公众号,回复: 9893564 查看本文章
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>example03</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
    <body>
        <h1>example03</h1>
        <hr>
        <div id="app">
            {{num}}
            <p><button @click="add">add</button></p>
        </div>
        <p><button onclick="reduce()">reduce</button></p>
        <p><button onclick="reduceOnce()">reduceOnce</button></p>
        <p><button onclick="off()">off</button></p>

        <script type="text/javascript">
            var app = new Vue({
                el:'#app',
                data:{
                    num:1
                },
                methods:{
                    add:function(){
                        this.num++;
                    }
                }
            });

            app.$on("reduce",function(){
                console.log("执行了reduce方法");
                this.num--;
            })

            app.$once('reduceOnce',function(){
                console.log('只执行一次的方法');
                this.num--;

            });

            function reduce(){
                app.$emit('reduce');
            }

            function reduceOnce(){
                app.$emit('reduceOnce');
            }

            //关闭事件
            function off(){
                app.$off('reduce');
            }
        </script>
    </body>
</html>

浏览器效果:

猜你喜欢

转载自www.cnblogs.com/Elva3zora/p/12510155.html
今日推荐