美文网首页
在vue3中如何父子组件都使用v-model

在vue3中如何父子组件都使用v-model

作者: 奈何明月照沟渠 | 来源:发表于2023-02-26 13:28 被阅读0次

子组件中使用modelValue 接收

const props = defineProps({
  modelValue: String
})
// 但不要直接在子组件中直接使用v-model=‘props.modelValue’ 虽然不影响功能 但是会一直报警告对于子组件来说props是只读属性
const val = ref(props.modelValue | '')
<input v-model='val' />

子组件中使用emit('update:modelValue') 来触发父组件更新

const emit =defineEmits<{
  (event: 'update:modelValue',val: String): void
}>

inpChange(e: keyboardEvent) => {
 emit("update:modelValue", e.target.value)
}

<input v-model='val'  @input='inpChange' />

相关文章

网友评论

      本文标题:在vue3中如何父子组件都使用v-model

      本文链接:https://www.haomeiwen.com/subject/uxfcldtx.html