vue3子组件修改父组件数据
vue3子组件修改父组件数据
·
使用 props + emit(推荐方式)
父组件:
<template>
<ChildComponent
:parent-data="parentData"
@update-data="handleUpdateData"
/>
<p>父组件数据: {{ parentData }}</p>
</template>
<script setup>
import { ref } from 'vue'
import ChildComponent from './ChildComponent.vue'
const parentData = ref('初始数据')
const handleUpdateData = (newValue) => {
parentData.value = newValue
}
</script>
子组件:
<template>
<button @click="updateParentData">修改父组件数据</button>
</template>
<script setup>
const props = defineProps(['parentData'])
const emit = defineEmits(['update-data'])
const updateParentData = () => {
emit('update-data', '子组件修改后的数据')
}
</script>
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)