modal not closing with event vue 3

I have a modal I am trying to close via button. The modal renders as expected and in the parent the console.log(”); does render but does not close

modal

Parent

<script setup lang="ts">
    
const showLoginModal = ref(false);
const closeModal = () => {
    showLoginModal.value = false;
    console.log("in the parent closeModal " + showLoginModal.value);
}


</script>
<template>
    <div v-if="showLoginModal">
        <LoginModal @close="closeModal"></LoginModal>
    </div>
</template>

In the child I have

<template>
    <div class="modal-overlay">
        <div class="modal-content">
            <button v-on:click="$emit('close', true)">Close</button>
        </div>
    </div>
</template>

My question is, why is my closeModal function unable to close the modal.

const showLoginModal = ref(false);

In the console logs I see the log, but it has no effect

const closeModal = () => {
    showLoginModal.value = false;
    console.log("in the closeModal " + showLoginModal.value);
}

I see the value is false, but it doesn’t have an effect on

    <div v-if="showLoginModal">
        <LoginModal @close="closeModal"></LoginModal>
    </div>

Which is declared in the parent.

  • Your modal code is hard to make any sense of. Maybe you’ve left some things out? showLoginModal is not defined in the child and is not passed down as a prop. The child component also has two closeModal functions,const closeModal arrow function and function closeModal. It’s not clear how either of them are invoked. I see the parent listens for the “close” emit and calls “closeModal” but has no closeModal function of its own. The single prop in the child course is seemingly unused. It’s very unclear how anything in your parent or child are related.

    – 

  • See the errors from just copy/pasting your modal component code?. The store and router I understand are just missing imports. The other errors can’t be rationalized

    – 

  • @yoduh I updated my question. My apologies for the confusion.

    – 

Leave a Comment