Home>Article>Web Front-end> Let’s talk about Vue’s event monitoring instruction v-on
In front-end development, we need to interact with users frequently. At this time, we must monitor user events, such as clicks, drags, keyboard events, etc.How to monitor events in Vue? Use thev-on
directive. The following article will take you through Vue's event monitoring command v-on. I hope it will be helpful to you!
Function: Binding Defined event listener
Abbreviation: @
Expected: Function (method) | Inline Statement (internal expression) | Object (object)
Parameters: event
Bind event listeners in Vue. The event type is specified by parameters; the expression can be the name of a method or an inline statement, which can be omitted if there are no modifiers. (Learning video sharing:vue video tutorial)
Starting fromv2.4.0
, v-on also supports binding an event/listener key-value pair without parameters Object. Note that when using object syntax, no modifiers are supported.
When used on ordinary elements, it can only listen to native DOM events. When used on a custom element component, you can also listen to custom events triggered by child components.
When listening to native DOM events, the method takes the event as the only parameter. If using an inline statement, the statement can access an $event attribute: v-on:click="handle('ok', $event)".
Let’s look at a simple example first
// JavaScript var app = new Vue({ el: '#app', methods: { clickMe: function() { alert("点击我,我就出来了!(^_^)") } }, data: { } })点击我
The effect you see is as follows:
In the Vue template, we usev-on
, and bind aclick
event (v-on:click
), and then bind thisclick
event An eventclickMe
was triggered. And thisclickMe
in Vue, we usually put it inmethods: {}
, that is to say, theclickMe
function is written inmethods
middle.
Seev-on:click="clickMe"
, is it very similar toonclock="clickMe"
in HTML? Judging from the appearance, their writing methods are different, but the results are the same. In Vue, we can also use@click
instead ofv-on:click
. Then they play the same role.
In Vue, in addition to the above examples, there are also the following usage methods forv-on
:
on subcomponents Listen to custom events (the event handler will be called when the subcomponent triggersmy-event
):
From the above simple example, you can see that Vue is in the process of event processing It comes with some modifiers:
.stop
: Call event.stopPropagation()
. prevent
: Call event.preventDefault()
.capture
: Use capture mode when adding event listeners
.self
: The callback is only triggered when the event is triggered from the element itself to which the listener is bound
.{keyCode | keyAlias }
: Trigger the callback only when the event is triggered from a specific key
.native
: Listen to the native event of the component root element
.once
: Only triggers the callback once
.left
: Only triggers when the left mouse button is clicked. (Only available in v2.2.0)
.right
: Triggered only when the right mouse button is clicked, (Only available on v2.2.0)
.middle
: Only triggered when the middle mouse button is clicked, (only available in v2.2.0)
.passive
: Add a listener in{passive: true}
mode, (only available in v2.3.0)
Vue’s official website handles events and customizes components events are described in detail. If you are interested, you can view the corresponding content:
Let’s make a simple effect, which is a counter. The effect is as follows:
This effect is very simple. Clickto increase the number, and click
-
to decrease the number.
In Vue, our template has three elements, two buttons, and a container that displays numbers. The first button does addition calculations, and the second button does subtraction counting. The simple structure is as follows:
{{ count }}
两个按钮上都绑定了一个click
事件,点击按钮分别触发increase
和reduce
两个函数,前者做加法,后者做减法。另外一个元素中我们有一个值{{ count }}
。每次点击按钮这个{{ count }}
都会做相应的变化。
模板有了之后,需要添加对应的功能。
let app = new Vue({ el: '#app', methods: { increase: function() { this.count++ }, reduce: function() { this.count-- } }, data: { count: 0 } })
在Vue中,我们在methods
中声明了两个函数,分别是increase
(加法)和reduce
(减法)。另外需要在数据源中声明count
。
演示demo地址:https://codepen.io/airen/pen/PJbKNg
对于这么简单的效果,我们也可以直接在v-on中处理完:
比如我们前面的示例,修改下:
{{ count }}// JavaScript let app = new Vue({ el: '#app', data: { count: 0 } })
效果一样:
演示demo地址:https://codepen.io/airen/pen/veyeLY
The above is the detailed content of Let’s talk about Vue’s event monitoring instruction v-on. For more information, please follow other related articles on the PHP Chinese website!