I have a custom event to close a modal window and it works perfectly as long as the component is a direct child of the parent. But when I want to call the event from a deeper nested component it doesn’t work.
Here is my parent component:
<Modal @modal-close="close" />
Here is my close button inside <Modal>
:
<a @click="$emit('modal-close')">CLOSE</a>
As said, this works. But inside <Modal>
I’m importing a component for the content of <Modal>
and now I want to call the event from inside the content component like this:
const emit = defineEmits(['modal-close']);
emit('modal-close');
I get no warning or errors. But the parent-parent component <Modal>
does nothing. Are events not available globaly? How can I solve this problem?
SOLVED:
I chose the non-event based way by creating a function in the parent for closing the <Modal>
and pass it as a prop to the children. It seems to me that this is the simplest solution and also provides flexibility to add custom actions when closing the modal.
This is my parent component now:
const close = (data) {
// close the modal
// process data
};
<Modal :close="close" />
And my child component:
const $$ = defineProps({
close: Boolean | Function,
});
<a @click="$$.close">CLOSE</a>