1

So i want to toggle my modal dialog from parent component and i tried each step mentioned here Stack Overflow Question about same Topic , but still my Modal Dialog is not visible and it even has undefined value when i see from VUE console. and in Elements section the modal dialog div is not created.

MY MODAL DIALOG is not showing in elements section on webpage, but showing in Vue console with undefined prop value. But Click effect is working from Parent component. and modal is setting true when i click on div.


My Parent Code

<template>
    <div class="collection">
        <section class="section section-elements">
            <div class="elements-outer" @click="openModal">
                 <CopyComponent v-bind:item="item"/>                            
           </div>
        </section>
        <modal v-modal="modal"/>
    </div>
</template>
<script>
import Modal from "../components/Modal.vue";
export default {
    name: 'Collection',
    components: {
        Modal
    },
    data() {
        return {
            modal: false
        }
    },
    methods: {
        openModal() {
            this.modal = !this.modal;
        }
    }

}
</script>

My Child Component

<template>
<div v-if="value" class="modal">
    <div class="body">
        body
    </div>
    <div class="btn_cancel" @click="modalToggle">
        <i class="icon icon-cancel" />
    </div>
</div>
</template>

 <script>
   export default {
   name: "Modal",
   props: ["value"],
   methods: {
    modalToggle() {
      this.$emit("input", !this.value);
    }
  }
};
</script>

am i missing anything?

Please help, thanks.

1
  • Both of answer worked if not using bootstrap, but if someone using Bootstrap then he has to remove default bootstrap classes and use custom classes with custom css.. For more info please visit: adamwathan.me/2016/01/04/… See accepted answer on this post: stackoverflow.com/questions/42890385/… Commented Apr 8, 2020 at 13:38

2 Answers 2

2

Your prop is called value so you need to pass it from parent component, also assign event for toggle modal.

<modal :value="modal" @toggle="modalToggle"/>

And in your child:

<template>
<div v-if="value" class="modal">
    <div class="body">
        body
    </div>
    <div class="btn_cancel" @click="modalToggle">
        <i class="icon icon-cancel" />
    </div>
</div>
</template>
<script>
   export default {
   name: "Modal",
   props: ["value"],
   methods: {
    modalToggle() {
      this.$emit("toggle");
    }
  }
}
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

The first reply has solved the problem... Value is changing, click effect is working, div is being created when i click.. but the modal dialog has display none by default because of modal class..Thats why it is not showing up, do you know how to solve it?
@gautam what about using vue-js-modal?
already tried show, not working... and don't wanna use extra plugin
What's happening when you have by default modal: true in your data and also class show?
2

You misspelled v-model in <modal v-modal="modal"/>, it should be <modal v-model="modal"/>

6 Comments

but still the modal dialog is not opening, it has display none in style under inspect element .. only the content is visible under inspect elements
It might be a problem in your css, try removing the modal class from your modal component. I tested your code myself whithout any styles and everything works fine.
but without having modal class, i wouldn't be able to inherit bootstrap default style and Functionality of modal dialog. i need that class
Are you using vue bootstrap? Because I'm almost sure that pure css bootstrap won't work as you plan.
Try vue-bootstrap instead, it should solve your problems and you will not need to build every component by yourself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.