Skip to content

Commit d58df8c

Browse files
shentaophanan
authored andcommitted
Fix #1802; Update docs to avoid misconceptions around emitting events (#1838)
1 parent 57623b2 commit d58df8c

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/v2/guide/components.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ Vue.component('blog-post', {
268268

269269
Now, whenever a new property is added to `post` objects, it will automatically be available inside `<blog-post>`.
270270

271-
## Sending Messages to Parents with Events
271+
## Listening to Child Components Events
272272

273273
As we develop our `<blog-post>` component, some features may require communicating back up to the parent. For example, we may decide to include an accessibility feature to enlarge the text of blog posts, while leaving the rest of the page its default size:
274274

@@ -323,15 +323,7 @@ The problem is, this button doesn't do anything:
323323
</button>
324324
```
325325

326-
When we click on the button, we need to communicate to the parent that it should enlarge the text of all posts. Fortunately, Vue instances provide a custom events system to solve this problem. To emit an event to the parent, we can call the built-in [**`$emit`** method](../api/#vm-emit), passing the name of the event:
327-
328-
```html
329-
<button v-on:click="$emit('enlarge-text')">
330-
Enlarge text
331-
</button>
332-
```
333-
334-
Then on our blog post, we can listen for this event with `v-on`, just as we would with a native DOM event:
326+
When we click on the button, we need to communicate to the parent that it should enlarge the text of all posts. Fortunately, Vue instances provide a custom events system to solve this problem. The parent can choose to listen to any event on the child component instance with `v-on`, just as we would with a native DOM event:
335327

336328
```html
337329
<blog-post
@@ -340,6 +332,16 @@ Then on our blog post, we can listen for this event with `v-on`, just as we woul
340332
></blog-post>
341333
```
342334

335+
Then the child component can emit an event on itself by calling the built-in [**`$emit`** method](../api/#vm-emit), passing the name of the event:
336+
337+
```html
338+
<button v-on:click="$emit('enlarge-text')">
339+
Enlarge text
340+
</button>
341+
```
342+
343+
Thanks to the `v-on:enlarge-text="postFontSize += 0.1"` listener, the parent will receive the event and update `postFontSize` value.
344+
343345
{% raw %}
344346
<div id="blog-posts-events-demo" class="demo">
345347
<div :style="{ fontSize: postFontSize + 'em' }">

themes/vue/source/js/common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
'Replacing-Merging-with-Existing-Attributes':
4747
'/v2/guide/components-props.html#Replacing-Merging-with-Existing-Attributes',
4848
'Custom-Events':
49-
'/v2/guide/components.html#Sending-Messages-to-Parents-with-Events',
49+
'/v2/guide/components.html#Listening-to-Child-Components-Events',
5050
'Using-v-on-with-Custom-Events':
51-
'/v2/guide/components.html#Sending-Messages-to-Parents-with-Events',
51+
'/v2/guide/components.html#Listening-to-Child-Components-Events',
5252
'Binding-Native-Events-to-Components':
5353
'/v2/guide/components-custom-events.html#Binding-Native-Events-to-Components',
5454
'sync-Modifier':

0 commit comments

Comments
 (0)