Answer the question
In order to leave comments, you need to log in
How to pass events from a component in a slot to the slot's parent component?
I am using vuejs3.
To implement a modal window (conditionally Modal.vue ) with motley content inside, I chose the approach with slots.
Everything was fine, but there is a component with a form (conditionally Form.vue ), upon submission of which it is necessary that this modal window closes.
A simplified implementation is as follows:
Modal.vue
<template>
<div class="modal" v-if="modalShow">
<span class="modal__close" @click="toggleModalShow">x</span>
<div class="modal__title">
<slot name='modal-title'></slot>
</div>
<div class="modal__content">
<slot name='modal-content'></slot>
</div>
</div>
</template>
<script lang="ts" setup>
import {ref} from 'vue'
export const modalShow = ref<boolean>(false)
export function toggleModalShow() {
modalShow.value = !modalShow.value
}
</script>
<template>
<form @submit="fuSubmit">
<button type="submit">Отправить</button>
</form>
</template>
<script lang="ts" setup>
export const fuSubmit = (e:any) => {
e.preventDefault();
}
</script>
<template>
<Modal>
<template v-slot:modal-title>Форма</template>
<template v-slot:modal-content>
<Form/>
</template>
</Modal>
</template>
<script lang="ts" setup>
</script>
<template>
<form @submit="fuSubmit">
<button type="submit">Отправить</button>
</form>
</template>
<script lang="ts" setup="{ emit }">
export const fuSubmit = (e:any) => {
emit('modal-close');
e.preventDefault();
}
</script>
Answer the question
In order to leave comments, you need to log in
I tried to transmit via emit, but it turned out to intercept the event only in App.vue, but not in Modal.vue
I have not worked with Vue3 yet, but in Vue2 EMNIP you can do something like this:
https://github.com/vuejs/vue/issues/4332#issuecomm...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question