Unable to check whether an event listener was passed once defineEmits is used #12528
Unanswered
cellador
asked this question in
Help/Questions
Replies: 1 comment
-
a workaround: const vnode = getCurrentInstance().vnode
const hasClickListener = vnode ? !!vnode.props.onClick : false see playground I think you should define an const props = defineProps({
onClick: {
type: Function,
default: () => {
// default click processing logic
}
}
}) If an |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Vue version
3.5.13
Link to minimal reproduction
https://play.vuejs.org/#eNqtUk1PAjEQ/StNY7KakN0Y9IIrUQkHPahREw/WmHUZoLDbNu0USTb73227gAgrJ2/tvDfz3nxU9FqpeGGB9mhqcs0VEgNoVZ8JXiqpkQxkqV45ToclRzLWsiRRnGwHfXp0sceXFltTVvF1Vpo0sk7QfRBKVWQI7kdI+kv6Ki94Pr9klNGkgT81Wb12NXe4abJVl3YomlyKMZ/EMyOF67zyRRjNXRVegH5QyKUwjPZIQDyWFYX8ugsx1BY663g+hXzeEp+ZpY8x+qjBgF4AoxsMMz0BbODh8z0s3XsDlnJkC8c+AD6BkYX1HhvajRUjZ3uLF9zehn1wMXkxwyWCMOumvFHPrAOfUbcJP8C/Wv+x243PQh4TtZvi7g20nNAIxlyAJ5jjtygsJXo/Obx0ozLRryoSSTEICYQLcpQhalPXLtHD+yttua4WP/+v+7EA7QfrxLrxeXzapfU3ZfYh+w==
Steps to reproduce
Check the resulting html
What is expected?
According to the documentation,
$attrs
collects fallthrough attributes. Therefore it is expected that after usingdefineEmits(['click'])
,onClick
is no longer present in$attrs
. This however means that besides going after the vnode, there is no API to know which listener was passed to a component oncedefineEmits
is used.What is actually happening?
The component which uses
defineEmits(['click'])
in its setup is no longer able to check whether the@click="..."
attribute was passed.System Info
No response
Any additional comments?
This makes it impossible to define a component which delivers a default click handler if no click handler is passed from the outside.
Beta Was this translation helpful? Give feedback.
All reactions