
在 vue3 + element-plus 中,子组件使用 this.$emit 向父组件发送消息,但在父组件中无法收到。
根据提供的代码,可以发现以下问题:
1. 子组件的事件名不正确
子组件中使用 this.$emit("conditionupdate") 触发自定义事件,但父组件的事件监听器却是 "update"。两者名称不一致导致消息发送失败。
立即学习“前端免费学习笔记(深入)”;
2. 子组件未注册事件
父组件需要在渲染子组件时注册事件监听器,才能接收子组件发送的消息。
子组件
<script>
import {ref} from 'vue';
export default {
name: 'newnew',
props: ['column', 'column_filter_type', 'select_items'],
data() {
return {
open_flag: ref(true),
condition: {
value_start: '',
value_end: ''
}
}
},
methods: {
confirm() {
this.open_flag = false;
// ...这里应该依据条件类型做校验
this.$emit('update', {
column: this.column,
column_filter_type: this.column_filter_type,
condition: this.condition
});
}
}
}
</script>父组件
<newnew
...
@update="conditionUpdate" // 注册事件监听器
/>通过以上修改,子组件可以正确向父组件发送消息,从而触发父组件中的 conditionupdate 方法。
以上就是VUE3 + element-plus 中,子组件使用 this.$emit 发送消息,父组件为什么接收不到?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号