Subscribe on changes!

How to dynamically add methods to the defineEmits() method

avatar
Mar 21st 2023

What problem does this feature solve?

Currently, there is a new requirement to encapsulate a dynamic button list that requires the method of dynamically configuring buttons. Previously, this was directly used in Vue2$ Emit() implementation, but in Vue 3.0, the declaration of defineEmits() is required now, but these methods need to be dynamically added. How do I implement it??

What does the proposed API look like?

暂无

avatar
Mar 21st 2023

you can define them as an empty array by default and add emits as props like:

const props = defineProps(['emits'])
defineEmits(props.emits ?? [])
avatar
Mar 21st 2023

you can define them as an empty array by default and add emits as props like:

const props = defineProps(['emits'])
defineEmits(props.emits ?? [])

That won't work.

avatar
Mar 21st 2023

Concerning the question: use one event, pass the dynamic event name in its payload.