vue2 vue3 自定义组件使用v-model双向绑定

父组件:

<childComp v-model="aaa" />
<script>
...
data() {
    return {
        aaa: 123
    }
}
...
</script>

自定义组件:

childComp.vue

<script>
...
props: {
  // vue2
  value: {
    type: Number,
    default: 999
  },
  // vue3
modelValue: {
        type: Number,
        default: 999
    },
},
data() {
    return {
        newVal: 0,
    }
},
methods: {
    fun() {
     // vue2
     this.$emit('input', this.newVal)
     // vue3
        this.$emit('update:modelValue', this.newVal)
    }
}
...
</script>

 

posted @ 2023-03-11 16:02  落花看风雪  阅读(20)  评论(0编辑  收藏  举报