Vue2
<template>
<div>
<childrenComp v-model="text"></childrenComp>
</div>
</template>
<script>
import childrenComp from '@/components/childrenComp.vue'
export default {
components: { childrenComp },
data() {
return {
text: ""
}
}
}
</script>
<template>
<div>
<span>{{value}}</span>
<button @click="changeValue">修改绑定值</button>
</div>
</template>
<script>
export default {
props:["value"],
methods:{
changeValue(){
this.$emit("input","onchange value")
}
}
}
</script>
Vue3
<script setup lang="ts">
import { ref } from 'vue';
import ChildrenComp from './ChildrenComp.vue'
const text = ref("")
</script>
<template>
<div>
<ChildrenComp v-model="text"></ChildrenComp>
</div>
</template>
<script setup lang="ts">
import { defineProps } from 'vue'
defineProps(["modelValue"])
</script>
<template>
<div>
<span>{{$props.modelValue}}</span>
<button @click="$emit('update:modelValue','onchange value')">修改绑定值</button>
</div>
</template>