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>