You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/components/events.md
+10-2Lines changed: 10 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -27,9 +27,17 @@ The parent can then listen to it using `v-on`:
27
27
<MyComponent @some-event="callback" />
28
28
```
29
29
30
-
Like components and props, event names provide an automatic case transformation. Notice we emitted a camelCase event, but is able to listen to it using a kebab-cased listener in the parent.
30
+
The `.once` modifier is also supported on component event listeners:
31
31
32
-
As with [props casing](/guide/components/props.html#prop-name-casing), we recommend using kebab-cased event listeners in templates.
32
+
```vue-html
33
+
<MyComponent @some-event.once="callback" />
34
+
```
35
+
36
+
Like components and props, event names provide an automatic case transformation. Notice we emitted a camelCase event, but is able to listen to it using a kebab-cased listener in the parent. As with [props casing](/guide/components/props.html#prop-name-casing), we recommend using kebab-cased event listeners in templates.
37
+
38
+
:::tip
39
+
Unlike native DOM events, component emitted events do **not** bubble. You can only listen to the events emitted by a direct child component.
Copy file name to clipboardExpand all lines: src/guide/essentials/event-handling.md
+33-65Lines changed: 33 additions & 65 deletions
Original file line number
Diff line number
Diff line change
@@ -6,14 +6,22 @@ aside: deep
6
6
7
7
## Listening to Events
8
8
9
-
We can use the `v-on` directive, which we typically shorten to the `@` symbol, to listen to DOM events and run some JavaScript when they're triggered. The usage would be `v-on:click="methodName"` or with the shortcut, `@click="methodName"`
9
+
We can use the `v-on` directive, which we typically shorten to the `@` symbol, to listen to DOM events and run some JavaScript when they're triggered. The usage would be `v-on:click="handler"` or with the shortcut, `@click="handler"`.
10
10
11
-
For example:
11
+
The handler value can be one of the following:
12
+
13
+
1.**Inline handlers:** Inline JavaScript to be executed when the event is triggered (similar to the native `onclick` attribute).
14
+
15
+
2.**Method handlers:** A property name or path that points to a method defined on the component.
16
+
17
+
## Inline Handlers
18
+
19
+
Inline handlers are typically used in simple cases, for example:
12
20
13
21
<divclass="composition-api">
14
22
15
23
```js
16
-
constcounter=ref(0)
24
+
constcount=ref(0)
17
25
```
18
26
19
27
</div>
@@ -22,16 +30,16 @@ const counter = ref(0)
22
30
```js
23
31
data() {
24
32
return {
25
-
counter:0
33
+
count:0
26
34
}
27
35
}
28
36
```
29
37
30
38
</div>
31
39
32
40
```vue-html
33
-
<button @click="counter++">Add 1</button>
34
-
<p>The button above has been clicked {{ counter }} times.</p>
41
+
<button @click="count++">Add 1</button>
42
+
<p>Count is: {{ count }}</p>
35
43
```
36
44
37
45
<divclass="composition-api">
@@ -45,9 +53,9 @@ data() {
45
53
46
54
</div>
47
55
48
-
## Method Event Handlers
56
+
## Method Handlers
49
57
50
-
The logic for many event handlers will be more complex though, so keeping your JavaScript in the value of the `v-on` attribute isn't feasible. That's why `v-on` can also accept the name of a method you'd like to call.
58
+
The logic for many event handlers will be more complex though, and likely isn't feasible with inline handlers. That's why `v-on` can also accept the name or path of a component method you'd like to call.
51
59
52
60
For example:
53
61
@@ -104,9 +112,13 @@ methods: {
104
112
105
113
</div>
106
114
115
+
A method handler automatically receives the native DOM Event object that triggers it - in the example above, we are able to access the element dispatching the event via `event.target.tagName`.
116
+
117
+
The template compiler detects method handlers by checking whether the `v-on` value string is a valid JavaScript identifier or property access path. For example, `foo`, `foo.bar` and `foo['bar']` are treated as method handlers, while `foo()` and `count++` are treated as inline handlers.
118
+
107
119
## Methods in Inline Handlers
108
120
109
-
Instead of binding directly to a method name, we can also use methods in an inline JavaScript statement:
121
+
Instead of binding directly to a method name, we can also call methods in an inline handler. This allows us to pass the method custom arguments instead of the native event:
110
122
111
123
<divclass="composition-api">
112
124
@@ -130,8 +142,8 @@ methods: {
130
142
</div>
131
143
132
144
```vue-html
133
-
<button @click="say('hi')">Say hi</button>
134
-
<button @click="say('what')">Say what</button>
145
+
<button @click="say('hello')">Say hello</button>
146
+
<button @click="say('bye')">Say bye</button>
135
147
```
136
148
137
149
<divclass="composition-api">
@@ -145,7 +157,7 @@ methods: {
145
157
146
158
</div>
147
159
148
-
Sometimes we also need to access the original DOM event in an inline statement handler. You can pass it into a method using the special `$event` variable:
160
+
Sometimes we also need to access the original DOM event in an inline handler. You can pass it into a method using the special `$event` variable:
149
161
150
162
```vue-html
151
163
<button @click="warn('Form cannot be submitted yet.', $event)">
@@ -182,46 +194,6 @@ methods: {
182
194
183
195
</div>
184
196
185
-
## Multiple Event Handlers
186
-
187
-
You can have multiple methods in an event handler separated by a comma operator like this:
188
-
189
-
```vue-html
190
-
<!-- both one() and two() will execute on button click -->
191
-
<button @click="one($event), two($event)">
192
-
Submit
193
-
</button>
194
-
```
195
-
196
-
<divclass="composition-api">
197
-
198
-
```js
199
-
functionone(event) {
200
-
// first handler logic...
201
-
}
202
-
203
-
functiontwo(event) {
204
-
// second handler logic...
205
-
}
206
-
```
207
-
208
-
</div>
209
-
<divclass="options-api">
210
-
211
-
212
-
```js
213
-
methods: {
214
-
one(event) {
215
-
// first handler logic...
216
-
},
217
-
two(event) {
218
-
// second handler logic...
219
-
}
220
-
}
221
-
```
222
-
223
-
</div>
224
-
225
197
## Event Modifiers
226
198
227
199
It is a very common need to call `event.preventDefault()` or `event.stopPropagation()` inside event handlers. Although we can do this easily inside methods, it would be better if the methods can be purely about data logic rather than having to deal with DOM event details.
@@ -230,8 +202,8 @@ To address this problem, Vue provides **event modifiers** for `v-on`. Recall tha
230
202
231
203
-`.stop`
232
204
-`.prevent`
233
-
-`.capture`
234
205
-`.self`
206
+
-`.capture`
235
207
-`.once`
236
208
-`.passive`
237
209
@@ -248,10 +220,6 @@ To address this problem, Vue provides **event modifiers** for `v-on`. Recall tha
248
220
<!-- just the modifier -->
249
221
<form @submit.prevent></form>
250
222
251
-
<!-- use capture mode when adding the event listener -->
252
-
<!-- i.e. an event targeting an inner element is handled here before being handled by that element -->
253
-
<div @click.capture="doThis">...</div>
254
-
255
223
<!-- only trigger handler if event.target is the element itself -->
256
224
<!-- i.e. not from a child element -->
257
225
<div @click.self="doThat">...</div>
@@ -261,26 +229,26 @@ To address this problem, Vue provides **event modifiers** for `v-on`. Recall tha
261
229
Order matters when using modifiers because the relevant code is generated in the same order. Therefore using `@click.prevent.self` will prevent **all clicks** while `@click.self.prevent` will only prevent clicks on the element itself.
262
230
:::
263
231
232
+
The `.capture`, `.once`, and `.passive` modifiers mirror the [options of the native `addEventListener` method](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters):
233
+
264
234
```vue-html
235
+
<!-- use capture mode when adding the event listener -->
236
+
<!-- i.e. an event targeting an inner element is handled here before being handled by that element -->
237
+
<div @click.capture="doThis">...</div>
238
+
265
239
<!-- the click event will be triggered at most once -->
266
240
<a @click.once="doThis"></a>
267
-
```
268
241
269
-
Unlike the other modifiers, which are exclusive to native DOM events, the `.once` modifier can also be used on [component events](/guide/components/events). If you haven't read about components yet, don't worry about this for now.
270
-
271
-
Vue also offers the `.passive` modifier, corresponding to [`addEventListener`'s `passive` option](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters).
272
-
273
-
```vue-html
274
242
<!-- the scroll event's default behavior (scrolling) will happen -->
275
243
<!-- immediately, instead of waiting for `onScroll` to complete -->
276
244
<!-- in case it contains `event.preventDefault()` -->
277
245
<div @scroll.passive="onScroll">...</div>
278
246
```
279
247
280
-
The `.passive` modifier is especially useful for improving performance on mobile devices.
248
+
The `.passive` modifier is typically used with touch event listeners for [improving performance on mobile devices](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#improving_scrolling_performance_with_passive_listeners).
281
249
282
250
::: tip
283
-
Don't use `.passive` and `.prevent` together, because `.prevent` will be ignored and your browser will probably show you a warning. Remember, `.passive` communicates to the browser that you _don't_ want to prevent the event's default behavior.
251
+
Do not use `.passive` and `.prevent` together, because `.passive` already indicates to the browser that you _do not_ intend to prevent the event's default behavior, and you will likely see a warning from the browser if you do so.
0 commit comments