Vue 事件监听

前端开发 作者: 2024-08-20 10:00:01
事件监听 v-on 使用v-on进行事件绑定监听,回调函数写在methods中。可以使用@的这种简写形式来代替v-on,当事件源无参数传递时,可省略括号。 语法如下所示: <button @:事

事件监听

<button @:事件=回调函数(参数)>点我</button>
<body>

<div id="app">
    <button @click="func">点我</button>
</div>

<script src="./vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",methods: {
            func() {
                console.log("点击事件发生了");
            }
        }
    })
</script>
</body>
<body>

<div id="app">
    <button @click="func($event)">点我</button>
</div>

<script src="./vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",methods: {
            func(ev) {
                console.log(ev);
            }
        }
    })
</script>
</body>

修饰符

<div id="app">
    <button @click.once="func">点我</button>
</div>

<script src="./vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",methods: {
            func() {
                console.log("只执行一次");
            }
        }
    })
</script>
</body>

<body>

<div id="app">
    <a href="http://www.google.com/" @click.prevent="func">点我</a>
</div>

<script src="./vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",methods: {
            func() {
                console.log("不执行默认事件");
            }
        }
    })
</script>
</body>
<style>
    .f {
        width: 100px;
        height: 100px;
        background-color: red;
        text-align: center;
        color: white;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .s {
        width: 50px;
        height: 50px;
        background: blue;
        text-align: center;
        color: white;
    }
</style>

<body>

<div id="app">
    <section @click="father" class="f">
        <!-- 父亲事件不会触发 -->
        <article @click.stop="son" class="s"></article>
    </section>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",data: {},methods: {
            son() {
                console.log("儿子事件触发");
            },father() {
                console.log("父亲事件触发")
            }
        },});
</script>
</body>
<style>
    .f {
        width: 100px;
        height: 100px;
        background-color: red;
        text-align: center;
        color: white;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .s {
        width: 50px;
        height: 50px;
        background: blue;
        text-align: center;
        color: white;
    }
</style>

<body>

<div id="app">
    <!-- 点儿子时父亲事件不会触发 -->
    <section @click.self="father" class="f">
        <article @click="son" class="s"></article>
    </section>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",});
</script>
</body>
<style>
    .f {
        width: 100px;
        height: 100px;
        background-color: red;
        text-align: center;
        color: white;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .s {
        width: 50px;
        height: 50px;
        background: blue;
        text-align: center;
        color: white;
    }
</style>
<body>

<div id="app">
    <section @click.capture="father" class="f">
        <article @click="son" class="s"></article>
    </section>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",});
</script>
</body>

键盘修饰符

<body>
    <div id="app">
        <input type="text" @keyup.ctrl.a="func">
    </div>
    <script src='./vue.js'></script>
    <script>
        "use strict";
        const app = new Vue({
            el: "#app",data: {
            },methods:{
                func(){
                    console.log("执行了");  
                },},});
    </script>
</body>

鼠标修饰符

<body>
    <div id="app">
        <section @contextmenu.prevent="func" :style={width:"300px",height:"300px",backgroundColor:"red"}>
        </section>
    </div>
    <script src='./vue.js'></script>
    <script>
        "use strict";
        const app = new Vue({
            el: "#app",});
    </script>
</body>

修饰符连用

<button @click.once.stop=func>点我</button>
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65453.html
Vue 事件监听