Skip to content

Commit

Permalink
feat: added warnings about v-model
Browse files Browse the repository at this point in the history
  • Loading branch information
NataliaTepluhina committed Feb 5, 2024
1 parent 77de31d commit 321453f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/api/sfc-script-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,23 @@ function inc() {
}
```

:::warning
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:

```js
// child component:
const model = defineModel({ default: 1 })

// parent component:
const myRef = ref()
```

```html
<Child v-model="myRef"></Child>
```

:::

### Modifiers and Transformers {#modifiers-and-transformers}

To access modifiers used with the `v-model` directive, we can destructure the return value of `defineModel()` like this:
Expand Down
19 changes: 18 additions & 1 deletion src/guide/components/v-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ const model = defineModel({ required: true })
const model = defineModel({ default: 0 })
```

:::warning
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:

```js
// child component:
const model = defineModel({ default: 1 })

// parent component:
const myRef = ref()
```

```html
<Child v-model="myRef"></Child>
```

:::

</div>

<div class="options-api">
Expand Down Expand Up @@ -211,7 +228,7 @@ const title = defineModel('title', { required: true })
<script setup>
defineProps({
title: {
required: true
required: true
}
})
defineEmits(['update:title'])
Expand Down

0 comments on commit 321453f

Please sign in to comment.