Skip to content

Commit 321453f

Browse files
feat: added warnings about v-model
Close vuejs/core#9987
1 parent 77de31d commit 321453f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/api/sfc-script-setup.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,23 @@ function inc() {
253253
}
254254
```
255255

256+
:::warning
257+
If you have a `default` value for `defineModel` prop and you don't provide any value for this prop from the parent component, it can cause a de-synchronization between parent and child components. In the example below, the parent's `myRef` is undefined, but the child's `model` is 1:
258+
259+
```js
260+
// child component:
261+
const model = defineModel({ default: 1 })
262+
263+
// parent component:
264+
const myRef = ref()
265+
```
266+
267+
```html
268+
<Child v-model="myRef"></Child>
269+
```
270+
271+
:::
272+
256273
### Modifiers and Transformers {#modifiers-and-transformers}
257274

258275
To access modifiers used with the `v-model` directive, we can destructure the return value of `defineModel()` like this:

src/guide/components/v-model.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ const model = defineModel({ required: true })
8484
const model = defineModel({ default: 0 })
8585
```
8686

87+
:::warning
88+
If you have a `default` value for `defineModel` prop and you don't provide any value for this prop from the parent component, it can cause a de-synchronization between parent and child components. In the example below, the parent's `myRef` is undefined, but the child's `model` is 1:
89+
90+
```js
91+
// child component:
92+
const model = defineModel({ default: 1 })
93+
94+
// parent component:
95+
const myRef = ref()
96+
```
97+
98+
```html
99+
<Child v-model="myRef"></Child>
100+
```
101+
102+
:::
103+
87104
</div>
88105

89106
<div class="options-api">
@@ -211,7 +228,7 @@ const title = defineModel('title', { required: true })
211228
<script setup>
212229
defineProps({
213230
title: {
214-
required: true
231+
required: true
215232
}
216233
})
217234
defineEmits(['update:title'])

0 commit comments

Comments
 (0)