探索 v-on 之奥秘一:事件及各类修饰符的深度剖析与案例展示

目录

v-on介绍

v-on事件修饰符

e.stopPropagation()、.stop、.self

​​​​​​​capture

​​​​​​​e.preventDefault()、.prevent

​​​​​​​.once

​​​​​​​.passive


v-on介绍


        在 Vue 中,v-on 乃是其所提供的用于事件绑定的一种机制,比如要给按钮添加一个单击事件,那么“v-on:click”能够被简写成“@click”。

        示例代码如下:

<body>  
    <div id="app">  
        <el-row>  
            <el-col :span="24">  
                <el-button v-on:click="showDataMsg">弹出data中msg数据</el-button>  
                <!-- 简写 -->  
                <el-button type="primary" @click="showTextContent">弹出textContent</el-button>  
                <el-button type="success" @click="showFunArg(msg,$event)">弹出传入的参数</el-button>  
                <!-- 动态事件 -->  
                <el-button @[event]="dynamicEvent">弹出操作的事件</el-button>  
                <!-- 对象语法 -->  
                <el-button @="{ mouseenter: doThis, mouseleave: doThat }">鼠标移入移出</el-button>  
            </el-col>  
            <el-col :span="24">  
                <el-button type="primary" round @click="add">数字加</el-button>  
                <el-button type="text">{
  {count}}</el-button>  
                <el-button type="success" round @click="sub">数字减</el-button>  
            </el-col>            
        </el-row>  
    </div>  
</body>  
<script>  
    const {createApp}=Vue;  
    const app=createApp({  
        data(){  
            return{  
                msg:'v-on',  
                count:0,  
                event:'click'  
            }  
        },  
        methods:{  
            showDataMsg(){  
                this.$message({  
                    showClose: true,  
                    message:this.msg,  
                })  
            },  
            showTextContent(e){  
                this.$message({  
                    showClose:true,  
                    message:e.target.innerText  
                })  
            },  
            showFunArg(arg1,arg2){  
                this.$message({  
                    showClose:true,  
                    message:'参数一:'+arg1  
                })  
                console.log(arguments,arg2.target.innerText);  
            },  
            add(){  
                this.count++;  
            },  
            sub(){  
                this.count--;  
            },  
            dynamicEvent(){  
                this.$message({  
                    showClose:true,  
                    message:'您操作的事件是:'+this.event  
                })  
            },  
            doThis(){  
                this.$message({  
                    showClose:true,  
                    message:'您操作的事件是:mouseenter'  
                })  
            },  
            doThat(){  
                this.$message({  
                    showClose:true,  
                    message:'您操作的事件是:mouseleave'  
                })  
            }  
        }  
    })  
    app.use(ElementPlus);  
    app.mount('#app');  
</script>  

        运行结果,截图所示:

        分析:

        单击按钮一弹出:“v-on”,单击按钮二弹出:“弹出textContent”

  • 19
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值