Skip to content

Commit 6581d52

Browse files
authored
vmodel modifiers clarify example and diff 2.x v 3.x (vuejs#344)
* clarify example and diff between 2.x and 3.x * fix modifiers link * remove 2.x reference * rephrase built in and remove native
1 parent ae5c772 commit 6581d52

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/guide/component-custom-events.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ app.component('user-name', {
141141

142142
## Handling `v-model` modifiers
143143

144-
In 2.x, we have hard-coded support for modifiers like `.trim` on component `v-model`. However, it would be more useful if the component can support custom modifiers. In 3.x, modifiers added to a component `v-model` will be provided to the component via the modelModifiers prop:
144+
When we were learning about form input bindings, we saw that `v-model` has [built-in modifiers](/guide/forms.html#modifiers) - `.trim`, `.number` and `.lazy`. In some cases, however, you might also want to add your own custom modifiers.
145+
146+
Let's create an example custom modifier, `capitalize`, that capitalizes the first letter of the string provided by the `v-model` binding.
147+
148+
Modifiers added to a component `v-model` will be provided to the component via the `modelModifiers` prop. In the below example, we have created a component that contains a `modelModifiers` prop that defaults to an empty object.
149+
150+
Notice that when the component's `created` lifecycle hook triggers, the `modelModifiers` prop contains `capitalize` and its value is `true` - due to it being set on the `v-model` binding `v-model.capitalize="bar"`.
145151

146152
```html
147153
<my-component v-model.capitalize="bar"></my-component>
@@ -166,7 +172,7 @@ app.component('my-component', {
166172
})
167173
```
168174

169-
We can check `modelModifiers` object keys and write a handler to change the emitted value. In the code below we will capitalize the string:
175+
Now that we have our prop set up, we can check the `modelModifiers` object keys and write a handler to change the emitted value. In the code below we will capitalize the string whenever the `<input />` element fires an `input` event.
170176

171177
```html
172178
<div id="app">
@@ -209,7 +215,7 @@ app.component('my-component', {
209215
app.mount('#app')
210216
```
211217

212-
For `v-model` with arguments, the generated prop name will be `arg + "Modifiers"`:
218+
For `v-model` bindings with arguments, the generated prop name will be `arg + "Modifiers"`:
213219

214220
```html
215221
<my-component v-model:foo.capitalize="bar"></my-component>

0 commit comments

Comments
 (0)