在组件上使用 v-model

65 阅读1分钟

需求:我们经常封装组件,需要v-model 绑定,特别是表单自定义控件的时候,做检验的时候特别需要,那么在vue3 中应该怎么做呢?

子组件的props 里面定义modelValue,在emit 中update 这个定义

props: {
    modelValue: {
      type: Object
    }
  }
  
  context.emit('update:modelValue',formState.value)

父组件在v-model 就可以使用了

<term-picker v-model="pickerVisable" :defaultDate="defaultDate" @confirm="confirm"> </term-picker>

如果觉得好,就给点打赏吧,1元钱也行。

image.png