You can use the v-on directive to bind and listen to DOM events. The content of the binding can be a method on the current instance (no parentheses are needed after it) or an inline expression. If a method is provided, the native DOM event will be passed in as the first parameter, and this event will have the targetVM attribute, pointing to the corresponding ViewModel that triggered the event:
<div id="demo"> <a v-on="click: onClick">触发一个方法函数</a> <a v-on="click: n++">触发一个表达式</a> </div>
new Vue({ el: '#demo', data: { n: 0 }, methods: { onClick: function (e) { console.log(e.targetVM.n); console.log(e.target.tagName);// "A" console.log(e.targetVM === this);// true } } });
Execute the expression
targetVM is useful when using v-on in v-repeat, because v-repeat will create a large number of child ViewModels. However, it is more convenient and intuitive to pass in the alias representing the current ViewModel data object by executing the expression:
<ul id="list"> <li v-repeat="item in items" v-on="click: toggle(item)"> {{item.text}} </li> <button v-on="click: submit('hello!', $event)">Submit</button> </ul>
new Vue({ el: '#list', data: { items: [ { text: 'one', done: true }, { text: 'two', done: false } ] }, methods: { toggle: function (item) { console.info(item.done); item.done = !item.done; console.info(item.done); }, submit: function (msg, e) { e.stopPropagation(); console.info(msg + ' submit is called!'); } } })
When you want to access the original DOM event in the expression, you can pass an $event parameter Go in.
key filter
When listening for keyboard events, we often need to determine commonly used key codes. Vue.js provides a special filter that can only be used with the v-on directive: key. It receives a parameter representing the key code and completes the judgment:
<!-- 只有当 keyCode 等于 13 时才调用方法 --> <input v-on="keyup:mySubmit | key 13">
The system has many preset values that can be used, for example:
<!-- 效果同上 --> <input v-on="keyup:submit | key 'enter'">
The default value is: enter tab delete esc up down left right space
Why is it used in HTML Listener
You may notice that the whole way of event listening goes against the traditional concept of "separation of concern". Don't worry, since all Vue.js event handlers and expressions are strictly bound to the current view's ViewModel, it won't cause any maintenance difficulties. In fact, using v-on has more benefits:
It makes it easy to locate the corresponding method implementation in JS code in the HTML template.
Because you don’t need to manually bind events in JS, your ViewModel code can be very pure logic, completely decoupled from the DOM. This will be easier to test.
When a ViewModel is destroyed, all event listeners will be automatically removed. You don’t have to worry about cleaning them yourself.
The above is the entire content of this article, I hope it will be helpful to everyone’s study

VueUse 是 Anthony Fu 的一个开源项目,它为 Vue 开发人员提供了大量适用于 Vue 2 和 Vue 3 的基本 Composition API 实用程序函数。本篇文章就来给大家分享几个我常用的几个 VueUse 最佳组合,希望对大家有所帮助!

Vue3如何更好地使用qrcodejs生成二维码并添加文字描述?下面本篇文章给大家介绍一下Vue3+qrcodejs生成二维码并添加文字描述,希望对大家有所帮助。

本篇文章给大家整理分享8个GitHub上很棒的的 Vue 项目,都是非常棒的项目,希望当中有您想要收藏的那一个。

如何使用VueRouter4.x?下面本篇文章就来给大家分享快速上手教程,介绍一下10分钟快速上手VueRouter4.x的方法,希望对大家有所帮助!

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor
