vue3子组件通过update事件修改父组件绑定的属性值,update:modelValue
vue3子组件通过update事件修改父组件绑定的属性值,update:modelValue注意事项
·
子组件:
<script setup lang="ts">
//父组件v-model默认绑定modelValue属性(建议)。如要更换属性名将v-model="searchText" 修改为v-model:newValue="searchText" (newValue为新名称),修改后子组件也要相应修改,此时子组件不能更新父组件的值。
const prop = defineProps(['modelValue'])
const emits = defineEmits(['update:modelValue'])
const handleChange = (e: string) => {
emits('update:modelValue', e)
}
</script>
<template>
<input v-model="modelValue" @update:model-value="handleChange"/>
</template>
父组件:
<script lang="ts" setup>
import { defineComponent, ref } from 'vue'
import vModel from '../../components/vModel/index.vue'
const searchText = ref({ name: 'kkkk' })
</script>
<template>
<vModel v-model="searchText.name" />
<vModel :modelValue="searchText.name" @update:modelValue="(newValue) => (searchText.name = newValue)" />
<h1>------------{{ searchText.name }} -------------</h1>
</template>
更多推荐
已为社区贡献1条内容
所有评论(0)