defineModel use type object #13088
Unanswered
304541805
asked this question in
Help/Questions
Replies: 1 comment 1 reply
-
没有,你为什么不传的时候就直接 // child.vue
// script setup
...
const value = defineModel('value')
const count = defineModel('count')
// const ... = defineModel(...)
... <script setup>
const data = reactive({value: 'abc', count: 4})
</script>
<template>
<child v-model:value="data.value" v-model:count="data.count" />
</template> 以下全部不推荐:( // father.vue
<script setup>
const origin = reactive({ name: 'ww' })
const data = toRefs(origin)
</script>
<template>
...
<child v-model="data" />
...
</template>
// child.vue
<script setup>
...
const model = defineModel()
...
</script>
<template>
...
<input v-model="model.name">
...
</template> // father.vue
<script setup>
const origin = reactive({ name: 'ww' })
const data = reacitve({ origin })
</script>
<template>
...
<child v-model="data" />
...
</template>
// child.vue
<script setup>
...
const model = defineModel()
...
</script>
<template>
...
<input v-model="model.origin.name">
...
</template>
// father.vue
<script setup>
const data = ref({ name: 'ww' })
</script>
<template>
...
<child v-model="data" />
...
</template>
// child.vue
<script setup>
...
const model = defineModel()
const name = computed({
get: () => model.value.name,
set(val) =>model.value = { ...model.value, name: val }
})
...
</script>
<template>
...
<input v-model="name">
...
</template> |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When the value of defineModel is an object, assigning values to its properties individually results in the loss of reactivity. However, I need this two-way binding. Currently, this is how I am addressing the issue. Is there a better approach? Will nested property reactivity be supported in the future?

Beta Was this translation helpful? Give feedback.
All reactions