Answer the question
In order to leave comments, you need to log in
Vue: Is it possible to subscribe to events of components added via slot?
I want to make a wrapper component for modal windows. A form component is inserted into the modal window via slot. The form can generate "cancel" and "success" events, on which the window must be closed. The window itself raises the "close" event. Now it looks like this:
<a @click="showModal = true">открыть форму</a>
<Modal v-if="showModal" @close="showModal = false">
<Form @success="showModal = false" />
</Modal>
<div class="modal">
<div class="modal__overlay"></div>
<div class="modal__body">
<slot />
</div>
</div>
<slot @success="close()" />
<Form modal="true" />
Answer the question
In order to leave comments, you need to log in
I will not answer exactly whether it is possible or not, but I can say for sure that this is very bad. Let's say I took your modal component and decided to add my form, and let's say it also emits a success event , and the modal closes according to your idea, but for me this will be a problem, because I don't want it to close right away, I have to change api my component
<a @click="showModal = true">открыть форму</a>
<Modal v-if="showModal" @close="showModal = false">
<MySuperForm @success="handleSubmit" />
</Modal>
this.$parent.$on(...)
this.$children.$on(...)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question