Skip to content

Commit 261c471

Browse files
committed
feedback on event handling
1 parent 193ac18 commit 261c471

File tree

2 files changed

+43
-67
lines changed

2 files changed

+43
-67
lines changed

src/guide/components/events.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ The parent can then listen to it using `v-on`:
2727
<MyComponent @some-event="callback" />
2828
```
2929

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:
3131

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.
40+
:::
3341

3442
## Event Arguments
3543

src/guide/essentials/event-handling.md

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ aside: deep
66

77
## Listening to Events
88

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"`.
1010

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:
1220

1321
<div class="composition-api">
1422

1523
```js
16-
const counter = ref(0)
24+
const count = ref(0)
1725
```
1826

1927
</div>
@@ -22,16 +30,16 @@ const counter = ref(0)
2230
```js
2331
data() {
2432
return {
25-
counter: 0
33+
count: 0
2634
}
2735
}
2836
```
2937

3038
</div>
3139

3240
```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>
3543
```
3644

3745
<div class="composition-api">
@@ -45,9 +53,9 @@ data() {
4553

4654
</div>
4755

48-
## Method Event Handlers
56+
## Method Handlers
4957

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.
5159

5260
For example:
5361

@@ -104,9 +112,13 @@ methods: {
104112

105113
</div>
106114

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+
107119
## Methods in Inline Handlers
108120

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:
110122

111123
<div class="composition-api">
112124

@@ -130,8 +142,8 @@ methods: {
130142
</div>
131143

132144
```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>
135147
```
136148

137149
<div class="composition-api">
@@ -145,7 +157,7 @@ methods: {
145157

146158
</div>
147159

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:
149161

150162
```vue-html
151163
<button @click="warn('Form cannot be submitted yet.', $event)">
@@ -182,46 +194,6 @@ methods: {
182194

183195
</div>
184196

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-
<div class="composition-api">
197-
198-
```js
199-
function one(event) {
200-
// first handler logic...
201-
}
202-
203-
function two(event) {
204-
// second handler logic...
205-
}
206-
```
207-
208-
</div>
209-
<div class="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-
225197
## Event Modifiers
226198

227199
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
230202

231203
- `.stop`
232204
- `.prevent`
233-
- `.capture`
234205
- `.self`
206+
- `.capture`
235207
- `.once`
236208
- `.passive`
237209

@@ -248,10 +220,6 @@ To address this problem, Vue provides **event modifiers** for `v-on`. Recall tha
248220
<!-- just the modifier -->
249221
<form @submit.prevent></form>
250222
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-
255223
<!-- only trigger handler if event.target is the element itself -->
256224
<!-- i.e. not from a child element -->
257225
<div @click.self="doThat">...</div>
@@ -261,26 +229,26 @@ To address this problem, Vue provides **event modifiers** for `v-on`. Recall tha
261229
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.
262230
:::
263231

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+
264234
```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+
265239
<!-- the click event will be triggered at most once -->
266240
<a @click.once="doThis"></a>
267-
```
268241
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
274242
<!-- the scroll event's default behavior (scrolling) will happen -->
275243
<!-- immediately, instead of waiting for `onScroll` to complete -->
276244
<!-- in case it contains `event.preventDefault()` -->
277245
<div @scroll.passive="onScroll">...</div>
278246
```
279247

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).
281249

282250
::: 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.
284252
:::
285253

286254
## Key Modifiers

0 commit comments

Comments
 (0)