Vue事件修饰符(.stop .once .prevent)

这篇博客介绍了Vue.js中用于事件处理的三个修饰符:.stop用于阻止事件冒泡,.once确保事件只触发一次,.prevent则用于阻止默认行为。通过示例代码展示了这些修饰符在实际场景中的应用,帮助开发者更好地理解和运用Vue的事件系统。
摘要由CSDN通过智能技术生成

.stop 阻止冒泡


<div id="app">
    <!-- 事件后加 .stop 阻止冒泡 -->
     <div @click="show('父')">父盒子

           <div @click.stop="show('子')">子盒子</div>
              
     </div>
</div>

<script>
    var vm = new Vue({
        el: '#app',
        data:{
            num: 0
        },
        methods:{
            show:function(info){
                    console.log(info);
                    console.log(event.target.innerHTML);
            }
        }

    })
</script>
    

.once 点击事件将只会触发一次

<div id="app">
     <!-- 点击事件将只会触发一次 -->
     <div @click.once="show">
     <button>A</button>
     <button>B</button>
     <button>C</button>
    </div>

</div>

<script>
    var vm = new Vue({
        el: '#app',
        data:{
            num: 0
        },
        methods:{
            show:function(info){
                    console.log(event.target.innerHTML);
            }
        }

    })
</script>

 .prevent  阻止对象默认行为 


<div id="app">
    <a @click.prevent="handle()"   href="http://baidu.com">去百度</a>
</div>

<script>
    var vm = new Vue({
        el: '#app',
        data:{
            num: 0
        },
        methods:{
            handle:function(){
                console.log('点了链接');
                //阻止对象默认行为
                //event.preventDefault()
            }
        }

    })
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值