We need one useListeners to get all the event functions passed in by the parent component #532
Replies: 0 comments 6 replies
-
All listeners are actually props with In that case you can do something like this: const props = defineProps<{
onConfirm: () => void
}>()
const handleConfirm = () => {
if (props.onConfirm) {
props.onConfirm()
} else {
// do some default actions
}
} You can even pass async listeners and use them as awaitable hooks, which can't be achieved with |
Beta Was this translation helpful? Give feedback.
-
I know this method can get around this issue but I don't want to use it because it's not a uniform use of events and if I have to use this method to do my own stuff then I really need official support for useListeners. In fact due to the lack of this api, I've given up on 0 configuration design for components for now |
Beta Was this translation helpful? Give feedback.
-
Agree! |
Beta Was this translation helpful? Give feedback.
-
Is it possible for useListener to be supported @yyx990803 |
Beta Was this translation helpful? Give feedback.
-
You can do this <script lang="ts" setup>
import { useAttrs } from 'vue'
// const emit = defineEmits(["confirm","cancel"]);
const attrs = useAttrs();
const methods = {
cancel_handle ()
{
(attrs.onCancel || dialog_close)()
},
confirm_handle ()
{
(attrs.onConfirm || dialog_confirm)()
},
};
</script> |
Beta Was this translation helpful? Give feedback.
-
What problem does this feature solve?
When I design a general purpose component, I want the component to have a basic default behavior without any properties or events, meaning that you can even give the component no properties or events and it will work. It would be nice to pass a property or event to a component when you think it needs to modify a part of its behavior, and then override the default behavior of its corresponding part. Unfortunately when I was designing this component I found that once I defined the list of events held by the current component via defineEmits, I couldn't get the event function passed by the parent in the current component, which made my attempt to override the default behavior of the events impossible
Of course I knew there had been offers early on, but I checked them back and forth and saw that this feature was not taken seriously, so I was hoping to show off my own scenarios with a new feature suggestion and hope vue would consider opening up useListeners to give developers more room to play
What does the proposed API look like?
Beta Was this translation helpful? Give feedback.
All reactions