text
In JS, "events" do not need to be bound. The browser comes with many events, and each event can be bound to the "processor (processing function)".
Event ==> User behavior ==> Event triggering the browser ==> Execute the corresponding event handling function
We usually passaddEventListenet
Method to bind the event to the processing function:
("click", function () { // do... }, flase);
It should be noted thataddEventListenet
It's just that the "binding" event handler function is not "execution"! The processing function will only be executed when the corresponding event is triggered.
In some cases, we need to separate the processing functions and need to change themthis
execution, this is because of the event handler functionthis
Pointing to the element itself means we cannot use it.call
To changethis
pointing, because.call
The method will execute the function immediately!
// This will cause the test to be executed immediately!("click", (this), flase);
// You can use the .bind method to point to this with confidence, because bind will not be executed immediately("click", (this), flase);
Vue's event handling binding
comeVue
,Vue
Providedv-on
Directives allow developers to bind event handling functions to elements, such as:
<div v-on:click="methodName"></div> <!-- Or use @click right v-on Abbreviation --> <div @click="handler"></div>
v-on
The value ofJS
expression:
<button @click="count += 1">Add</button>
This is not recommended, the logic should be put intomethods
Processed inside.
v-on
The value can also be a method event handler:
<button @click="onClickAdd">Add</button>
@click
Accepts a method name or a call to a method.
When binding the event handler function through the method name, we can pass parameters to the method:
<div> <h1>{{ count }}</h1> <button @click="onClickAdd(2)">Add</button> <button @click="onClickMinus(2, $event)">Minus</button> </div>
The above code,button
BoundedonClickAdd
andonClickMinus
The function will not be executed immediately after processing, but will be executed only after the event is triggered. This will start with what we said.addEventListenet
It's the same!
andonClickAdd
andonClickMinus
The brackets are for the convenience of passing parameters we want to pass inmethdos
Processing in.
Here is a special parameter$event
, this property isVue
An internal encapsulation attribute that represents the event object that triggers the event.
const app = { template: `<button @click="onClickMinus(2, $event)">Minus</button>`, data(){ return { count: 0 } } methods:{ onClickMinus(num, e){ (e); += num; } } }
v-on
Directives can also bind multiple event handlers at the same time, for example:
<div> <h1>{{ count }}</h1> <button @click="onClickAdd(),setLog()">Add</button> <button @click="onClickMinus">Minus</button> </div>
Just use it between the two methods,
By separating, two event handling functions can be bound at the same time.
Event modifier
We may call when processing events()
or()
It's very common.Vue
A set of "modifiers" is provided to help us complete these events. The purpose of this is to allow developers to focus more on data logic without having to deal with it.DOM
Details of the event.
The modifier is a directive suffix represented by ., which contains the following:
-
.stop
: Stop the event from bubbles. -
.prevent
: The default behavior of blocking events. -
.self
:whenThe event handler will be triggered only when it is the element itself.
-
.capture
: Use event capture. -
.once
: After the event is called once, the listener will be automatically removed. -
.passive
:Modifier is generally used for touch events listeners and can be usedImprove the scrolling performance of mobile devices。
⚠️ Attention
Cannot be used at the same time.passive
and.prevent
,because.passive
The default behavior of the browser that you do not want to block events has been shown.
If you do,.prevent
Will be ignored and the browser will throw a warning.
<!-- Clicking the event will stop passing --> <a @="doThis"></a> <!-- Submit event will no longer reload the page --> <form @="onSubmit"></form> <!-- Modifications can be written in chain --> <a @="doThat"></a> <!-- There are also modifiers --> <form @></form> <!-- Only when The event handler will be triggered when it is the element itself. --> <!-- For example:Event handlers do not come from child elements --> <div @="doThat">...</div>
Pay attention to the order of call when using modifiers, because the relevant code is generated in the same order. Therefore use@
will block the default behavior of all click events of the element and its children, and@
This will only prevent the default behavior of click events to the element itself.
Key modifier
We may need to passTo determine the user triggered keys,
Vue
Allowed inv-on
or@
Add a key modifier when listening for key events, for example:
<!-- Only in `key` for `Enter` Called on time `submit` --> <input @="submit" />
You can use it directlyThe exposed key name is used as a modifier, but needs to be converted to the kebab-case form.
<input @-down="onPageDown" />
Vue
Alias are provided for some commonly used keys:
.enter
.tab
-
.delete
(capture"Delete
"and"Backspace
"Two buttons) .esc
.space
.up
.down
.left
.right
You can use the following system key modifiers to trigger the mouse or keyboard event listener, which will only be triggered when the key is pressed.
.ctrl
.alt
.shift
.meta
For example:
<!-- Alt + Enter --> <input @="clear" /> <!-- Ctrl + Click --> <div @="doSomething">Do something</div>
Please note that the system key modifier is different from the regular key. andkeyup
When events are used together, the key must be pressed when the event is emitted.
in other words,It will only hold it when you still hold it
ctrl
But it is triggered when another key is released. If you let go alonectrl
The key will not fire.
.exact
Modifiers allow control of the system key modifiers required to trigger an event.
<!-- When pressed Ctrl hour,即使同hour按下 Alt or Shift It will also trigger --> <button @="onClick">A</button> <!-- 仅When pressed Ctrl 且未按任何其他键hour才会触发 --> <button @="onCtrlClick">A</button> <!-- 仅当没有按下任何系统按键hour触发 --> <button @="onClick">A</button>
You can use the following mouse button modifiers to trigger the mouse event listener:
.left
.right
.middle
These modifiers limit the handler to events triggered by a specific mouse button.
Why does Vue listen for events in HTML?
We foundVue
Yestemplate
The direct binding of events in the event violates the concept of "separation of concerns". But don't worry,Vue
Strictly bind event processing methods and expressions to the current viewViewModel
He will not cause any maintenance difficulties.
When we usev-on
There are several benefits to binding event processing on view:
1. Scan theHTML
Template can be easily positionedJavaScript
The corresponding method inside.
2. No need to be thereJavaScript
Manually bind events to yoursViewModel
Code can be non-pure logic, andDOM
Completely decoction, easier to test.
3. Be aViewModel
When destroyed, all event processors will be automatically deleted. You need to worry about how to clean them up.
The above is a detailed explanation of the binding example of Vue event handling function. For more information about binding of Vue event handling function, please pay attention to my other related articles!