第十二课 Vue中的事件修饰符

Vue中的事件修饰符

事件修饰符的使用场景较多,主要为了处理事件冒泡和默认事件等所带来的一系列问题

事件修饰符

1)阻止默认事件

复制代码
    <div id="app">
            <a href="/" v-on:click.prevent="fun()">点击我弹出弹窗</a>
    </div>
    <script>
        new Vue({
            el: '#app',
            methods: {
                fun(){
                    alert('Hello World !');
                }
            }
        })
    </script>  

2)阻止冒泡

复制代码
        <style>
        .per{
            width: 200px;
            height: 200px;
            border: 2px solid #000;
        }

        .son{
            width: 100px;
            height: 100px;
            background: red;
        }
        </style>
        <div id="app">
                <div class="per" @click="per()">
                    <div class="son" @click.stop="son()"></div>
                </div>
        </div>
        <script>
            new Vue({
                el: '#app',
                methods: {
                    per(){
                        alert('This is per!');
                    },
                    son(){
                        alert('This is son!');
                    }                    
                }
            })
        </script>  

3)链式调用

复制代码
        <style>
        .per{
            width: 200px;
            height: 200px;
            border: 2px solid #000;
        }

        .son{
            width: 100px;
            height: 100px;
            background: red;
            display: block;
        }
        </style>
        <div id="app">
                <div class="per" @click="per()">
                    <a class="son" href="/" @click.prevent.stop="son()"></a>
                </div>
        </div>
        <script>
            new Vue({
                el: '#app',
                methods: {
                    per(){
                        alert('This is per!');
                    },
                    son(){
                        alert('This is son!');
                    }                    
                }
            })
        </script>  

4)事件捕获模式

复制代码
        <style>
        .per{
            width: 200px;
            height: 200px;
            border: 2px solid #000;
        }

        .son{
            width: 100px;
            height: 100px;
            background: red;
        }
        </style>
        <div id="app">
                <div class="per" @click="per()">
                    <div class="son" @click.capture="son()"></div>
                </div>
        </div>
        <script>
            new Vue({
                el: '#app',
                methods: {
                    per(){
                        alert('This is per!');
                    },
                    son(){
                        alert('This is son!');
                    }                    
                }
            })
        </script>  
  1. 其他不常用事件修饰符

a)self

b)once

相关推荐
zzzzzz3106 小时前
36K stars 之外:如何阅读 react-bits 这样的动画交互式 React 项目
javascript·react.js·动效
ljt27249606616 小时前
Flutter笔记--envied
前端·笔记·flutter
Flynt6 小时前
React Router v8 升了之后,我花了两天才把项目跑起来
前端·typescript·preact
泯泷12 小时前
手搓JSVMM第 8 篇:函数调用:参数、返回值与调用帧
前端·javascript·前端框架
Sherotree13 小时前
Google Antigravity——Agent 被提到主界面
前端·ide·后端·edge浏览器
Mr数据杨13 小时前
【Codex】用学生入学评估模块建立新生能力画像
android·java·javascript·django·codex·项目开发
里欧跑得慢13 小时前
Flutter 像素级还原:设计稿到代码的视觉无损转换技巧
前端·css·flutter·web
里欧跑得慢14 小时前
Flutter 三方库 function_tree — 鸿蒙应用开发中的动态数学公式解析与计算神器,实现鸿蒙深度适配下的复杂函数逻辑运行时求值实战(适配鸿蒙 HarmonyOS Next ohos)
android·前端·安全·flutter·华为·harmonyos
IT毕设实战小研14 小时前
基于安卓的空气质量查询系统
android·大数据·vue.js·爬虫·python·django·课程设计
苏灿烤鱼14 小时前
没有机密文件,也没有付费数据,在浏览器里造一颗"间谍卫星"
前端·javascript·github