Vue学习记录-11

Vue学习记录-11

1、学习代码

<!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">
    <script src="../js/vue.js"></script>
    <title>事件修饰符</title>
    <style>
        *{
            margin-top: 20px;
        }
        .demo1{
            height: 50px;
            background-color: aquamarine;
        }
        .box1{
            padding: 5px;
            background-color: aqua;
        }
        .box2{
            padding: 5px;
            background-color: yellowgreen;
        }
        .box3{
            padding: 5px;
            background-color: aqua;
        }
        .box4{
            padding: 5px;
            background-color: yellowgreen;
        }
        .list{
            height: 200px;
            width: 200px;
            background-color: yellow;
            overflow: auto;
        }
        li{
            height: 100px;
        }

    </style>
</head>
<body>
    <div id="root">
        <h2>欢迎来到{{name}}学习</h2>
        <!-- prevent即事件修饰符:阻止默认事件 -->
        <a :href="url" @click.prevent="showInfo">点我提示信息</a>
        <!-- stop:阻止事件冒泡 -->
        <div class="demo1" @click="showInfo">
            <button @click.stop="showInfo">点我提示信息</button>
        </div>
        <!-- once:事件只触发一次 -->
        <div class="demo2">
            <button @click.once="showInfo">点我提示信息</button>
        </div>
        <!--  使用事件的捕获模式-->
        <div class="box1" @click="showMsg('box1')">
            div1
            <div class="box2" @click="showMsg('box2')">
                div2
            </div>
        </div>
        <!-- slef:只有当是对该组件进行操作时才会触发方法否则不执行 -->
        <div class="box3" @click.self="showMsg('box3')">
            div3
            <div class="box4" @click="showMsg('box4')">
                div4
            </div>
        </div>
<!-- passive: 如果我定义的函数很耗时影响了组件本身的效果事件,则组件本身的效果事件不会等待我定义的事件执行完毕而会立即执行-->
<!-- scroll/wheel两种方式 wheel会受到自定义函数的影响scroll不会-->
        <ul class="list" @scroll="demo">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>

    </div>
    <script>
        const vm = new Vue({
            el:'#root',
            data:{
                name:'北方工业大学',
                url:'http://www.baidu.com'
            },
            methods:{
                showInfo(e){
                    // e.preventDefault()//阻止默认行为
                    alert('同学你好')
                },
                showMsg(msg){
                    alert(msg)
                },
                demo(){
                    console.log('滚动')
                    for(let i = 0;i<100000;i++){
                        console.log('#')
                    }
                    console.log('累坏了')
                }
            }
        })
    </script>
</body>
</html>

2、学习总结

Vue中的事件修饰符:

  1. prevent:阻止默认事件(常用);
  2. stop:阻止事件冒泡(常用);
  3. once:事件只触发一次(常用);
  4. capture:使用事件的捕获模式;
  5. self:只有event.target是当前操作的元素时才触发事件;
  6. passive:事件的默认行为(组件的效果)立即执行,无需等待事件回调执行完毕;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值