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/v2/guide/components.md
+12-10Lines changed: 12 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -268,7 +268,7 @@ Vue.component('blog-post', {
268
268
269
269
Now, whenever a new property is added to `post` objects, it will automatically be available inside `<blog-post>`.
270
270
271
-
## Sending Messages to Parents with Events
271
+
## Listening to Child Components Events
272
272
273
273
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:
274
274
@@ -323,15 +323,7 @@ The problem is, this button doesn't do anything:
323
323
</button>
324
324
```
325
325
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
-
<buttonv-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:
335
327
336
328
```html
337
329
<blog-post
@@ -340,6 +332,16 @@ Then on our blog post, we can listen for this event with `v-on`, just as we woul
340
332
></blog-post>
341
333
```
342
334
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
+
<buttonv-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.
0 commit comments