1.通过v-model
<template>
<child-component v-model="val" />
</template>
<template>
<input type="text" v-model="childValue" @input="handleClick"/>
</template>
export default {
model: {
prop: 'anyKey',
event: 'anyEventName'
},
props: {
anyKey: {
type: Number,
default: 0,
required: true
}
},
data(){
return {
childValue: this.anykey
}
},
watch: {
anyKey(newValue){
this.childValue = newValue;
}
},
methods: {
handleClick() {
this.$emit('anyEventName', this.childValue)
}
}
}
export default {
props: {
value: {
type: Number,
default: 0,
required: true
}
},
data(){
return {
childValue: this.value
}
},
wacth: {
value(newVal){
this.chilcValue = newVal;
}
},
methods: {
handleClick() {
this.$emit('input', this.childValue)
}
}
}
2.通过.sync修饰符
<template>
<child-component :val.sync="val" />
</template>
<template>
<input type="text" v-model="childValue" @input="handleClick"/>
</template>
export default {
props: {
val: {
type: Number,
default: 0,
required: true
}
},
data(){
return {
childValue: this.val
}
},
watch: {
val(newValue){
this.childValue = newValue;
}
},
methods: {
handleClick() {
this.$emit('update:val', this.childValue)
}
}
}