Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义事件冒泡问题 #13243

Closed
blcyzycc opened this issue Mar 15, 2025 · 0 comments
Closed

自定义事件冒泡问题 #13243

blcyzycc opened this issue Mar 15, 2025 · 0 comments

Comments

@blcyzycc
Copy link

我尝试写一个可多层嵌套的组件,并模拟dom树的事件冒泡功能,如下:

<script setup> import { defineProps, defineEmits } from 'vue'

const emit = defineEmits(['onTest'])

const onClick = () => {
let re = emit('onTest')
console.log(re);
}
</script>

当我在父组件通过 @ontest="onTest2" 并在 onTest2 中返回值,
父组件中的代码:
const onTest2 = () => {
return 1
}

发现 let re = emit('onTest') 并不能接收到,这样自定义事件冒泡效果就无法事件。

只能只能将事件方法通过 v-bind 绑定,以属性的形式传入,这样虽然能在 子组件中通过 props.onTest() 触发事件并接收到返回值。
但是如果 子组件是通过 v-for 循环渲染的,那么事件传参又是个问题,所有有没有更好的方式实现这个功能呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant