How to Trigger a Modal Dialog from the Parent Component in VUE

I have been trying to toggle my modal dialog from the parent component using the steps mentioned in a related Stack Overflow Question. However, I am facing an issue where my modal dialog is not visible and appears to have an undefined value when checked in the VUE console. Additionally, the modal dialog div is not being created in the Elements section of the webpage.

The modal dialog is not showing in the elements section on the webpage, but it does appear in the Vue console with an undefined prop value. Strangely enough, the click effect is working from the Parent component, as the modal is set to true upon clicking the div.


Parent Component 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-model="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>

Child Component Code

<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>

Is there something I might be missing?

Your help would be greatly appreciated. Thank you.

Answer №1

Make sure to pass the prop named value from the parent component and set up an event listener for toggling the modal.

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

In your 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("toggle");
    }
  }
}
</script>

Answer №2

The correct syntax for adding two-way data binding in Vue.js is using v-model instead of v-modal. So, make sure to change <modal v-modal="modal"/> to <modal v-model="modal"/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mastering the Art of Binding Option Data from API Responses in Vue.js

Just starting out with Vue.js and attempting to bind option data from an API response. I've made an axios call in the mounted() hook and assigned the companies data from the response, but I'm encountering errors as shown below. 373:11 error ...

What is preventing me from using memoization in the getServerSideProps of NextJS?

I'm currently using React along with NextJS to showcase a variety of products on a specific category page. While I am able to successfully fetch the products utilizing getServerSideProps, I am not fond of how it continuously requests the product cata ...

Guide to resolving the error message "Server encountered a 502 status code when attempting to load resource. Utilizing Nuxt.js for troubleshooting"

Unfortunately, there seems to be an issue with this particular application. The error lies within the application's code and not the platform it is running on. Error Code: 502 BAD_GATEWAY Description: NO_STATUS_CODE_FROM_LAMBDA ID: iad1:wxl75-156037 ...

"Encountering issues with jQuery Ajax: Dealing with HTTP 405 or

When accessing a web service using jQuery Ajax that uses .asmx, I am encountering HTTP 405 and sometimes 500 errors. Is this a problem with the web service or on the client side? Please provide guidance with an example. $j.ajax({ type: ...

What are the ways to utilize vue-i18n setup within and beyond Vue components when working with Quasar?

Hello, fellow developers. I am currently working on implementing internationalization in Quasar, using Vue 3 (Composition API) and vue-i18n. My goal is to make internationalization available throughout the entire application, not just within Vue components ...

Enhance the slider's worth

Hey there, just wanted to mention that I didn't write this code myself. Another person did, and I'm a bit lost trying to figure out how to assign values to the three sliders. I don't want a default value; I want each slider to take its value ...

What is the process for retrieving the text element from a React component when using React.cloneElement?

Can I centralize a translation function for all table header elements? const CustomersTable = () => { var headers=<> <th>Name</th> <th>Age</th> <th>Another text</th> </> ...

The power of RXJS's combineLatest operator

Upon using the combineLatest operator, I encountered an unexpected response when adjusting the interval duration of the first and second observables. Here is an example of the code: let intObs1$ = interval(1000).pipe(take(3)); let intObs2$ = interval( ...

Encountered an error while attempting to load resource in Node.js

I'm currently working on a project utilizing Node js (Express...). Let me do my best to explain the issue I am encountering. My work is being done on localhost, and the main page of my index.html has buttons that lead to other pages when clicked. I c ...

Leverage slot-specific information within the component's script in Vue

In my Vue project, I faced an issue where Component A has a slot that passes an object as slot-scoped to Component B: Template of Component A: <template> <div> <slot :myObject="myObject" /> </div> </template> Template of ...

Guide to setting up a horizontal button menu and dropdown submenu beneath the navigation bar using Bootstrap 4

How can I design a horizontal button menu with submenus below the navigation bar in Bootstrap 4? Take a look at the image below to see what I'm aiming for: https://i.sstatic.net/j6b8a.png https://i.sstatic.net/rmtrM.png If you have any ideas or su ...

Tips for controlling the size of a canvas element: setting minimum and maximum width and height properties

function convertImageResolution(img) { var canvas = document.createElement("canvas"); if (img.width * img.height < 921600) { // Less than 480p canvas.width = 1920; canvas.height = 1080; } else if (img.width * img.he ...

What are some techniques for streamlining this code with Typescript?

I am currently working with the following code snippet: let doNavigate = this.currentScreen === removedFqn; if (doNavigate) { location.reload(); } Does anyone have any suggestions on how I can simplify this code using Typescript? ...

Can a directive be designed to function as a singleton?

Our large single page app includes a directive that is utilized in various locations across the page, maintaining its consistent behavior and appearance each time. However, we are experiencing an issue with this directive due to the ng-repeat it contains, ...

Implementing single execution of asynchronous functions in express

Can anyone provide guidance on how to ensure asynchronous functions are only executed once within an Express application? I'd like to avoid running this function on every route and am seeking a more express-friendly approach. var packageJson = false ...

Error in JSON Format Detected in Google Chrome Extension

Struggling with formatting the manifest for a simple Chrome extension I'm developing. I've been bouncing back and forth between these two resources to try and nail down the correct syntax: https://www.sitepoint.com/create-chrome-extension-10-mi ...

retrieve the child element's value of the directive

Having trouble with this directive: app.directive("myarticle",function($timeout){ return { restrict: "E", replace: true, templateUrl: "templates/article.html", link: function(scope, element, attrs) { element.getVal() } , ...

Customizing animations for birds

I'm currently in the process of developing a new website. The link to access the site is: One thing I am eager to achieve is having birds from this specific link incorporated into my background: I have attempted various methods without success. If a ...

Localized Error: The property 'clientWidth' cannot be retrieved as the object is null or undefined

The issue with the error message seems to only occur on Internet Explorer, and unfortunately there doesn't seem to be a clear solution at the moment. Even after setting http-equiv="X-UA-Compatible" to IE8, the problem persists and I cannot seem to re ...

Is there a way to automate clicking a button when an ajax request is successful through programming?

I am currently working on implementing an ajax cart feature for an e-commerce website. The specific functionality I am aiming to achieve is that when a user clicks the 'add to cart' button, the item is added to the cart and then the user is autom ...