Skip to content

Commit 5adbc5f

Browse files
sdraschrisvfritz
authored andcommitted
Moving Components Page into its Own Category (#1482)
* create components sidebar * break up first two sections * break it up further * split up the rest * clean up * initial pass, creating new, much slimmer components intro page that links to more in-depth pages * finish initial component registration page * finish initial refactor of component props page * beginning work on components custom events page * finish initial components custom events page * finish initial components slots page * finish initial dynamic and async components page * finish initial dynamic-async and edge-cases components pages * components review and tweaks up to the props page * fix components mistakes found by @phanan * fix typo in components-edge-cases * reorder Compiliation Scope in components-slots * fix link in components basics * fix codeblock language typo in components * add example of local registration in es2015 modules * add keep-alive example * remove redundant word in components * make dom template parsing caveats h2 * change simple example to base example in components * add async import example to circular references * fix typo in components-custom-events * remove redundand language in componenents-custom-events * add missing comma in components-custom-events * restate context in component-dynamic-async * remove redundant actuallys in components-dynamic-async * clarify .sync modifier usage * set up hash redirects for all old components sections * remove note about intentional single root elements * fix dom template parsing caveats example * fix closing </code> tags in components-edge-cases * fix typo in components-edge-cases * fix typo in components-edge-cases * remove extra 'a' in components-registration * add name casing and various tweaks to components-registration * complete custom v-model example in components-custom-events * minor tweaks to components pages * rename signature to definition for clarity
1 parent 9d1a534 commit 5adbc5f

14 files changed

+2375
-1481
lines changed

_config.yml

-1
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,3 @@ alias:
187187
examples/svg.html: v2/examples/svg.html
188188
examples/todomvc.html: v2/examples/todomvc.html
189189
examples/tree-view.html: v2/examples/tree-view.html
190-

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vuejs.org",
33
"private": true,
44
"hexo": {
5-
"version": "3.4.2"
5+
"version": "3.6.0"
66
},
77
"scripts": {
88
"start": "hexo server",

src/v2/api/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ type: api
14321432
14331433
- **Details:**
14341434
1435-
Contains parent-scope `v-on` event listeners (without `.native` modifiers). This can be passed down to an inner component via `v-on="$listeners"` - useful when creating higher-order components.
1435+
Contains parent-scope `v-on` event listeners (without `.native` modifiers). This can be passed down to an inner component via `v-on="$listeners"` - useful when creating transparent wrapper components.
14361436
14371437
## Instance Methods / Data
14381438
+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
title: Custom Events
3+
type: guide
4+
order: 103
5+
---
6+
7+
> This page assumes you've already read the [Components Basics](components.html). Read that first if you are new to components.
8+
9+
## Event Names
10+
11+
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. For example, if emitting a camelCased event name:
12+
13+
```js
14+
this.$emit('myEvent')
15+
```
16+
17+
Listening to the kebab-cased version will have no effect:
18+
19+
```html
20+
<my-component v-on:my-event="doSomething"></my-component>
21+
```
22+
23+
Unlike components and props, event names will never be used as variable or property names in JavaScript, so there's 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 `v-on:myEvent` would become `v-on:myevent` -- making `myEvent` impossible to listen to.
24+
25+
For these reasons, we recommend you **always use kebab-case for event names**.
26+
27+
## Customizing Component `v-model`
28+
29+
> New in 2.2.0+
30+
31+
By default, `v-model` on a component uses `value` as the prop and `input` as the event, but some input types such as checkboxes and radio buttons may want to use the `value` attribute for a [different purpose](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Value). Using the `model` option can avoid a conflict in such cases:
32+
33+
```js
34+
Vue.component('base-checkbox', {
35+
model: {
36+
prop: 'checked',
37+
event: 'change'
38+
},
39+
props: {
40+
checked: Boolean
41+
},
42+
template: `
43+
<input
44+
type="checkbox"
45+
v-bind:checked="checked"
46+
v-on:change="$emit('change', $event.target.value)"
47+
>
48+
`
49+
})
50+
```
51+
52+
Now when using `v-model` on this component:
53+
54+
```js
55+
<base-checkbox v-model="lovingVue"></base-checkbox>
56+
```
57+
58+
the value of `lovingVue` will be passed to the `checked` prop. The `lovingVue` property will then be updated when `<base-checkbox>` emits a `change` event with a new value.
59+
60+
<p class="tip">Note that you still have to declare the <code>checked</code> prop in component's <code>props</code> option.</p>
61+
62+
## Binding Native Events to Components
63+
64+
There may be times when you want to listen directly to a native event on the root element of a component. In these cases, you can use the `.native` modifier for `v-on`:
65+
66+
```html
67+
<base-input v-on:focus.native="onFocus"></base-input>
68+
```
69+
70+
This can be useful sometimes, but it's not a good idea when you're trying to listen on a very specific element, like an `<input>`. For example, the `<base-input>` component above might refactor so that the root element is actually a `<label>` element:
71+
72+
```html
73+
<label>
74+
{{ label }}
75+
<input
76+
v-bind="$attrs"
77+
v-bind:value="value"
78+
v-on:input="$emit('input', $event.target.value)"
79+
>
80+
</label>
81+
```
82+
83+
In that case, the `.native` listener in the parent would silently break. There would be no errors, but the `onFocus` handler wouldn't be called when we expected it to.
84+
85+
To solve this problem, Vue provides a `$listeners` property containing an object of listeners being used on the component. For example:
86+
87+
```js
88+
{
89+
focus: function (event) { /* ... */ }
90+
input: function (value) { /* ... */ },
91+
}
92+
```
93+
94+
Using the `$listeners` property, you can forward all event listeners on the component to a specific child element with `v-on="$listeners"`. For elements like `<input>`, that you also want to work with `v-model`, it's often useful to create a new computed property for listeners, like `inputListeners` below:
95+
96+
```js
97+
Vue.component('base-input', {
98+
inheritAttrs: false,
99+
props: ['label', 'value'],
100+
computed: {
101+
inputListeners: function () {
102+
var vm = this
103+
// `Object.assign` merges objects together to form a new object
104+
return Object.assign({},
105+
// We add all the listeners from the parent
106+
this.$listeners,
107+
// Then we can add custom listeners or override the
108+
// behavior of some listeners.
109+
{
110+
// This ensures that the component works with v-model
111+
input: function (event) {
112+
vm.$emit('input', event.target.value)
113+
}
114+
}
115+
)
116+
}
117+
},
118+
template: `
119+
<label>
120+
{{ label }}
121+
<input
122+
v-bind="$attrs"
123+
v-bind:value="value"
124+
v-on="inputListeners"
125+
>
126+
</label>
127+
`
128+
})
129+
```
130+
131+
Now the `<base-input>` component is a **fully transparent wrapper**, meaning it can be used exactly like a normal `<input>` element: all the same attributes and listeners will work.
132+
133+
## `.sync` Modifier
134+
135+
> New in 2.3.0+
136+
137+
In some cases, we may need "two-way binding" for a prop. Unfortunately, true two-way binding can create maintenance issues, because child components can mutate the parent without the source of that mutation being obvious in both the parent and the child.
138+
139+
That's why instead, we recommend emitting events in the pattern of `update:my-prop-name`. For example, in a hypothetical component with a `title` prop, we could communicate the intent of assigning a new value with:
140+
141+
```js
142+
this.$emit('update:title', newTitle)
143+
```
144+
145+
Then the parent can listen to that event and update a local data property, if it wants to. For example:
146+
147+
```html
148+
<text-document
149+
v-bind:title="doc.title"
150+
v-on:update:title="doc.title = $event"
151+
></text-document>
152+
```
153+
154+
For convenience, we offer a shorthand for this pattern with the `.sync` modifier:
155+
156+
```html
157+
<text-document v-bind:title.sync="doc.title"></text-document>
158+
```
159+
160+
The `.sync` modifier can also be used with `v-bind` when using an object to set multiple props at once:
161+
162+
```html
163+
<text-document v-bind.sync="doc"></text-document>
164+
```
165+
166+
This passes each property in the `doc` object (e.g. `title`) as an individual prop, then adds `v-on` update listeners for each one.
167+
168+
<p class="tip">Using <code>v-bind.sync</code> with a literal object, such as in <code>v-bind.sync="{ title: doc.title }"</code>, will not work. If you want to include multiple, unrelated data properties in the same <code>v-bind.sync</code>, we recommend creating a computed property that returns an object.</p>

0 commit comments

Comments
 (0)