Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

defineModel use type object #13088

Unanswered
304541805 asked this question in Help/Questions
Mar 22, 2025 · 1 comments · 1 reply
Discussion options

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?
image

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

没有,你为什么不传的时候就直接v-model = "input.value"
要多个这样写不就好了

// 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>

以下全部不推荐:(
1.

// 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>
  1. 这个比上面的好一点:<
// 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>
You must be logged in to vote
1 reply
Comment options

我明白你的意思如果只有一两个我单独处理就好,但是我是子组件其实是个大表单form,我以为defineModel可以简洁一下,而且我发现原始input 只是失去响应式,我用element的input根本无法输入所以我放弃了,用个ref做转换了,谢谢你的回答

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /