Skip to content

docs(v-model): same-name shorthand usage #3217

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/api/built-in-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,33 @@ Create a two-way binding on a form input element or a component.
- `<textarea>`
- components

- **Shorthand:**
- Omitting value (when argument and bound variable have the same name, requires 3.6+)

- **Modifiers**

- [`.lazy`](/guide/essentials/forms#lazy) - listen to `change` events instead of `input`
- [`.number`](/guide/essentials/forms#number) - cast valid input string to numbers
- [`.trim`](/guide/essentials/forms#trim) - trim input

- **Example**

```vue-html
<!-- form elements -->
<input v-model="text" />
<select v-model="option" />
<textarea v-model="message" />

<!-- component bindings -->
<MyComponent v-model="value" />
<MyComponent v-model:checked="checked" />

<!-- same-name shorthand (3.6+) -->
<MyComponent v-model:checked />
```

The shorthand only works when the bound variable has the same name as the argument. It cannot be used without an argument or when no binding expression is provided (e.g., `<input v-model>` will still result in an error).

- **See also**

- [Form Input Bindings](/guide/essentials/forms)
Expand Down
8 changes: 6 additions & 2 deletions src/guide/essentials/template-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ Attributes that start with `:` may look a bit different from normal HTML, but it

### Same-name Shorthand {#same-name-shorthand}

- Only supported in 3.4+
- `v-bind`: Supported since 3.4
- `v-model`: Supported since 3.6

If the attribute has the same name with the JavaScript value being bound, the syntax can be further shortened to omit the attribute value:

Expand All @@ -76,9 +77,12 @@ If the attribute has the same name with the JavaScript value being bound, the sy

<!-- this also works -->
<div v-bind:id></div>

<!-- same as v-model:foo="foo" -->
<input v-model:foo>
```

This is similar to the property shorthand syntax when declaring objects in JavaScript. Note this is a feature that is only available in Vue 3.4 and above.
This works similarly to JavaScript’s object property shorthand and helps keep templates concise.

### Boolean Attributes {#boolean-attributes}

Expand Down