Skip to content

Commit 2706494

Browse files
Changed a recommendation for custom events name casing (#754)
* fix: fixed custom event casing in guide * Update src/guide/component-custom-events.md Co-authored-by: skirtle <65301168+skirtles-code@users.noreply.github.com> Co-authored-by: skirtle <65301168+skirtles-code@users.noreply.github.com>
1 parent 9d48e28 commit 2706494

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

src/guide/component-basics.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ Which can be used in the template to control the font size of all blog posts:
178178
```html
179179
<div id="blog-posts-events-demo">
180180
<div :style="{ fontSize: postFontSize + 'em' }">
181-
<blog-post v-for="post in posts" :key="post.id" :title="post.title"></blog-post>
181+
<blog-post
182+
v-for="post in posts"
183+
:key="post.id"
184+
:title="post.title"
185+
></blog-post>
182186
</div>
183187
</div>
184188
```
@@ -216,7 +220,7 @@ When we click on the button, we need to communicate to the parent that it should
216220
Then the child component can emit an event on itself by calling the built-in [**`$emit`** method](../api/instance-methods.html#emit), passing the name of the event:
217221

218222
```html
219-
<button @click="$emit('enlarge-text')">
223+
<button @click="$emit('enlargeText')">
220224
Enlarge text
221225
</button>
222226
```
@@ -230,7 +234,7 @@ We can list emitted events in the component's `emits` option:
230234
```js
231235
app.component('blog-post', {
232236
props: ['title'],
233-
emits: ['enlarge-text']
237+
emits: ['enlargeText']
234238
})
235239
```
236240

@@ -241,7 +245,7 @@ This will allow you to check all the events that a component emits and optionall
241245
It's sometimes useful to emit a specific value with an event. For example, we may want the `<blog-post>` component to be in charge of how much to enlarge the text by. In those cases, we can pass a second parameter to `$emit` to provide this value:
242246

243247
```html
244-
<button @click="$emit('enlarge-text', 0.1)">
248+
<button @click="$emit('enlargeText', 0.1)">
245249
Enlarge text
246250
</button>
247251
```

src/guide/component-custom-events.md

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,17 @@
44
55
## Event Names
66

7-
Unlike components and props, event names don't provide any automatic case transformation. Instead, the name of an emitted event must exactly match the name used to listen to that event.
8-
9-
```js
10-
this.$emit('my-event')
11-
```
12-
13-
```html
14-
<my-component @my-event="doSomething"></my-component>
15-
```
16-
17-
If we're emitting a camelCased event name:
7+
Like components and props, event names provide an automatic case transformation. If you emit an event from the child component in camel case, you will be able to add a kebab-cased listener in the parent:
188

199
```js
2010
this.$emit('myEvent')
2111
```
2212

23-
Listening to the kebab-cased version will have no effect:
24-
2513
```html
26-
<!-- Won't work -->
2714
<my-component @my-event="doSomething"></my-component>
2815
```
2916

30-
Since event names will never be used as variable or property names in JavaScript, there is no reason to use camelCase or PascalCase. Additionally, `v-on` event listeners inside DOM templates will be automatically transformed to lowercase (due to HTML's case-insensitivity), so `@myEvent` would become `@myevent` -- making `myEvent` impossible to listen to.
31-
32-
For these reasons, we recommend you **always use kebab-case for event names**.
17+
As with [props casing](/guide/component-props.html#prop-casing-camelcase-vs-kebab-case), we recommend using kebab-cased event listeners when you are using in-DOM templates. If you're using string templates, this limitation does not apply.
3318

3419
## Defining Custom Events
3520

@@ -39,7 +24,7 @@ Emitted events can be defined on the component via the `emits` option.
3924

4025
```js
4126
app.component('custom-form', {
42-
emits: ['in-focus', 'submit']
27+
emits: ['inFocus', 'submit']
4328
})
4429
```
4530

0 commit comments

Comments
 (0)