-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add v-model changes to migration guide #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7651f2f
feat: added a v-model changes overview
NataliaTepluhina 9d6b7b4
feat: added separation for priority
NataliaTepluhina 3b3b9f1
fix: reverted categorization and fixed v-model
NataliaTepluhina b8b576d
Update src/guide/migration/v-model.md
NataliaTepluhina 5b1b49c
Merge branch 'master' into v-model-migration
NataliaTepluhina 5ce9886
Merge branch 'master' into v-model-migration
NataliaTepluhina 67c22c5
Update src/guide/migration/v-model.md
NataliaTepluhina 53ca3cf
Update src/guide/migration/v-model.md
NataliaTepluhina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# Changes to `v-model` on custom components | ||
|
||
## Overview | ||
|
||
In terms of what has changed, at a high level: | ||
|
||
- When used on custom components, `v-model` prop and event default names are changed: | ||
- prop: `value` -> `modelValue`; | ||
- event: `input` -> `update:modelValue`; | ||
- Multiple `v-model` bindings on the same component are possible now; | ||
- **BREAKING:** `v-bind`'s `.sync` modifier and component `model` option are removed and replaced with an argument on `v-model`; | ||
- **NEW:** Added the ability to create custom `v-model` modifiers. | ||
|
||
For more information, read on! | ||
|
||
## Introduction | ||
|
||
In Vue 2, the `v-model` directive required developers to always use the `value` prop. And if developers required different props for different purposes, they would have to resort to using `v-bind.sync`. In addition, this hard-coded relationship between `v-model` and `value` led to issues with how native elements and custom elements were handled. | ||
|
||
In 2.2 we introduced the `model` component option that allows the component to customize the prop and event to use for `v-model`. However, this still only allowed a single `v-model` to be used on the component. | ||
|
||
With Vue 3, the API for two-way data binding is being standardized in order to reduce confusion and to allow developers more flexibility with the `v-model` directive. | ||
|
||
## Previous Syntax | ||
|
||
In v2, using a `v-model` on a component was an equivalent of passing a `value` prop and emitting an `input` event: | ||
|
||
```html | ||
<ChildComponent v-model="pageTitle" /> | ||
|
||
<!-- would be shorthand for: --> | ||
|
||
<ChildComponent :value="pageTitle" @input="pageTitle = $event" /> | ||
``` | ||
|
||
If we wanted to change prop or event names to something different, we would need to add a `model` option to `ChildComponent` component: | ||
|
||
```html | ||
<!-- ParentComponent.vue --> | ||
|
||
<ChildComponent v-model="pageTitle" /> | ||
``` | ||
|
||
```js | ||
// ChildComponent.vue | ||
|
||
export default { | ||
model: { | ||
prop: 'title', | ||
event: 'change' | ||
}, | ||
props: { | ||
// this allows using the `value` prop for a different purpose | ||
value: String, | ||
// use `title` as the prop which take the place of `value` | ||
title: { | ||
type: String, | ||
default: 'Default title' | ||
} | ||
} | ||
} | ||
``` | ||
|
||
So, `v-model` in this case would be a shorthand to | ||
|
||
```html | ||
<ChildComponent :title="pageTitle" @change="pageTitle = $event" /> | ||
``` | ||
|
||
### Using `v-bind.sync` | ||
|
||
In some cases, we might need "two-way binding" for a prop (sometimes in addition to existing `v-model` for the different prop). To do so, we recommended emitting events in the pattern of `update:myPropName`. For example, for `ChildComponent` from the previous example with the `title` prop, we could communicate the intent of assigning a new value with: | ||
|
||
```js | ||
this.$emit('update:title', newValue) | ||
``` | ||
|
||
Then the parent could listen to that event and update a local data property, if it wants to. For example: | ||
|
||
```html | ||
<ChildComponent :title="pageTitle" @update:title="pageTitle = $event" /> | ||
``` | ||
|
||
For convenience, we had a shorthand for this pattern with the .sync modifier: | ||
|
||
```html | ||
<ChildComponent :title.sync="pageTitle" /> | ||
``` | ||
|
||
## Current Syntax | ||
|
||
In v3 `v-model` on the custom component is an equivalent of passing a `modelValue` prop and emitting an `update:modelValue` event: | ||
|
||
```html | ||
<ChildComponent v-model="pageTitle" /> | ||
|
||
<!-- would be shorthand for: --> | ||
|
||
<MyBook :modelValue="pageTitle" @update:modelValue="pageTitle = $event" /> | ||
``` | ||
|
||
### `v-model` arguments | ||
|
||
To change a model name, instead of a `model` component option, now we can pass an _argument_ to `v-model`: | ||
|
||
```html | ||
<ChildComponent v-model:title="pageTitle" /> | ||
|
||
<!-- would be shorthand for: --> | ||
|
||
<ChildComponent :title="pageTitle" @update:title="pageTitle = $event" /> | ||
``` | ||
|
||
 | ||
|
||
This also serves as a replacement to `.sync` modifier and allows us to have multiple `v-model`s on the custom component. | ||
|
||
```html | ||
NataliaTepluhina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<ChildComponent v-model:title="pageTitle" v-model:content="pageContent" /> | ||
|
||
<!-- would be shorthand for: --> | ||
|
||
<ChildComponent | ||
:title="pageTitle" | ||
@update:title="pageTitle = $event" | ||
:content="pageContent" | ||
@update:content="pageContent = $event" | ||
/> | ||
``` | ||
|
||
### `v-model` modifiers | ||
|
||
In addition to v2 hard-coded `v-model` modifiers like `.trim`, now v3 supports custom modifiers: | ||
|
||
```html | ||
<ChildComponent v-model.capitalize="pageTitle" /> | ||
``` | ||
|
||
Read more about custom `v-model` modifiers in the [Custom Events](../component-custom-events.html#handling-v-model-modifiers) section. | ||
|
||
## Next Steps | ||
|
||
For more information on the new `v-model` syntax, see: | ||
|
||
- [Using `v-model` on Components](../component-basics.html#using-v-model-on-components) | ||
- [`v-model` arguments](../component-custom-events.html#v-model-arguments) | ||
- [Handling `v-model` modifiers](../component-custom-events.html#v-model-arguments) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.