Troubleshooting: Vue component does not refresh data when a function is

In my application, I have created two Vue components:

Vue.component('event', {
    props:['event'],
    template: `
        <span class="pointer" @click="showModal = true">
            {{event.evname}}
            <modal @hide='hideModal' :event="event" :showModal="showModal">
                <div slot="title">{{event.evname}}</div>
                <div slot="content">{{event}}</div>
            </modal>
        </span>`,
    data: function(){
        return{
            showModal: false
        }
    },
    methods: {
        hideModal: function(){
            this.showModal = false
        }
    }
})

and

Vue.component('modal', {
    props:['event', 'showModal'],
    template: `
        <div v-if="showModal" class="modalBack">
            <div class="container modalPopup">
                <div class="row">
                    <span class="col-lg-11"><slot name="title"></slot></span><span class="pointer col-lg-1" @click="hide">X</span>
                    <slot name="content"></slot>
                </div>
            </div>
        </div>`,
    methods: {
        hide: function(){
            this.$emit('hide')
        }
    }
})

Even though the modal displays correctly when I click on the event name, there seems to be an issue when I try to close the event by clicking the 'X' button on the modal. The 'hide' event is being emitted and the hideModal method in the event component is being executed, but the modal remains visible. Upon checking the console, I noticed that 'this.showModal' displays false after attempting to close it, however, when I log 'this' and inspect showModal, it still shows true.

Do you have any insights on what might be causing this behavior? The intended functionality is for showModal to be set to false, which should trigger the modal component to close.

Answer №1

The issue you're facing lies in the fact that the modal is nested within the span element, which triggers the click event to set showModel = true. This means that when you attempt to close the modal by clicking the X button, you inadvertently also trigger the click event on the span. This results in showModal being set to false momentarily before being set back to true.

To resolve this problem, relocate the modal outside of the span element.

template: `
  <div>
    <span class="pointer" @click="showModal = true">
        {{event.evname}}
    </span>
    <modal @hide='hideModal' :event="event" :showModal="showModal">
       <div slot="title">{{event.evname}}</div>
       <div slot="content">{{event}}</div>
    </modal>
  </div>
`,

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

how to adjust the width of table columns dynamically using ng-repeat and AngularJS

Working on a user interface that contains a table where the column widths adjust according to percentage data. I'm using ng-repeat to populate the table, but need help setting unique widths for each piece of data received. Here's some of my Angul ...

What is the best way to loop through properties of a Typescript interface?

I am currently working with an interface called FilterData, which has the following structure: export interface FilterData { variables?: string[]; processDefinitionKey?: string; } When I make a request to the server, I receive an object named filterS ...

Is there a way to locate and refine the blogs associated with a specific user?

Check out the following blog entries stored in the Database: [ { _id: 5fec92292bbb2c32acc0093c, title: 'Boxing ring', author: 'T. Wally', content: 'boxing stuff', likes: 0, user: { _id: 5fd90181 ...

SVGs do not display on iPhones

Having some issues with my code - specifically, I have an SVG animation that seems to work on all devices except iPhones. I've been struggling to find a solution. Here is the code snippet for the SVG: <div class="contenuto-svg"> <svg vi ...

Encountering Issues with NextJS Dynamic SSR: Mobile Devices stuck on loading screen

Issue: The dynamic import feature of Next JS is encountering loading issues specifically on mobile browsers such as Google Chrome and Safari on IOS. Strangely, the functionality works smoothly on desktop browsers like Google Chrome and Mozilla. The projec ...

What are the steps to ensure that $.getScript functions properly?

It seems like the $.getScript function in jQuery will be really helpful once it's functioning properly, but I'm having some trouble with the last part. I have a feeling that the issue is related to how I'm loading JS files. How can I resolve ...

angular5: The ngFor directive will only function properly after the second button click

Here is my current situation: 1) When the user inputs a keyword in a text field and clicks on the search icon, it triggers an HTTP request to retrieve the data. 2) The retrieved data is then rendered in HTML using ngFor. The issue I am facing is that up ...

Getting the most out of Nexmo with multiple websocket connections

I have integrated the code provided by Nexmo (shown below) into my server. However, I am facing an issue where if two callers ping my server, the binary data from the second caller also streams into the same websocket endpoint, resulting in two binary st ...

Revamping the values attribute of a table embedded in a JSP page post an AJAX invocation

I am encountering an issue with displaying the content of a table. The table's data is retrieved via an AJAX request when clicking on a row in another table on the same page. Here is my code for the JSP page: <table id="previousList" class="table" ...

Encountering the error message "Vue CLI service command is not recognized" while attempting to run a Vue application

When I run the following commands in the root directory of my Vue app (version 2.6.12) rm -rf node_modules npm install npm run serve An error occurs with the message: sh: vue-cli-service: command not found To resolve this issue, I have to manually crea ...

Having trouble executing react-native project using the command "npx react-native run-android"

Hey there! I've been working on a react-native project for quite some time now and everything was going smoothly until yesterday. I encountered an error when running the command npx react-native run-android in Metro before the project had fully execut ...

Ways to resolve the error "Expected an assignment or function call but found an expression with no-unused-expressions"

Issue : Error in ./src/components/main.js Line 7: No function call or assignment found, only an expression is present (no-unused-expressions) Search for the specific keywords to get more information on each error. import React from 'react'; ...

Manage the lineup of tasks in the bull queue by organizing them into groups

I am currently working on a nodejs application that handles queues using the bull library. The application is required to make multiple asynchronous HTTP requests and then process the results of these calls. I'm curious about whether bull would be an ...

What is the best way to terminate a "for await ...of" loop if it fails to finish within a specified timeframe?

Is it possible to stop an asynchronous loop if it doesn't finish within a set time frame? I'm working with the following code: (async()=>{ for await(let t of asynDataStreamOrGenerator){ //some data processing } //some other code I n ...

Update image attributes (such as src, alt, etc.) after a successful AJAX request (and also help me troubleshoot my AJAX

The image link I am currently using is: echo "<br/><div class='right' id='post" . $id . "'> <img id='thumb' src='/images/like.png' title='Like it?' alt='Like button' onC ...

What is the best way to save a JavaScript object or JSON within an HTML element using the HTML5 data attribute?

I have an anchor element and I want to store and retrieve the object within it in this specific format. <a id ="test" data-val="{key1:val1,key1:val1}"> </a> When saved in this manner, fetching it with $("#test").data('val') returns ...

Background Patterns on Webpages

My website has a lovely gradient background on the html tag in css, while the body tag showcases a seamless pattern repeating on both the x and y axes. Everything was looking great until I checked the website on an iPad/iPhone in portrait mode, where the ...

Enhancing w3-import-html with JavaScript

<div id="import" includeHTML="page.html"></div> function getInclude() { var x = document.getElementById("import").includeHTML; //returns 'undefined' alert(x); } function modInclude() { document.getElementById("import") ...

JavaScript Await Dynamic User Input

I have implemented a modified version of a script for generating random numbers in multiple columns of a spreadsheet, inspired by Tim Cooper's solution on stackoverflow. This script works by selecting a range of cells and running it from an onOpen men ...

What is the reason for the Vue effect not being executed through the scheduler upon initialization of the effect?

What is the reason for the vue effect not going through the scheduler upon initialization? effect( () => { console.log('effect'); }, { scheduler: (job) => { console.log('run by scheduler'); job(); }, ...