The issue persists with UIkit modal element remaining in the DOM even after the parent component in Vue.js is destroyed

In my Vue app, I am utilizing the UIKit modal.

UIkit.modal(element).show(); // This adds the class uk-open and sets style to display:block
UIkit.modal(element).hide(); 

When I hide the modal, it simply removes the class uk-open and the inline style of display:block. I have observed that even after the parent component of the modal is destroyed, the modal element remains in the DOM. Additionally, upon calling the show method, the modal element is moved to the bottom of the body just before the closing body tag. Initially, on first load, it would be positioned where it was declared within the component. Could this relocation be the reason why it goes unseen and subsequently not removed? The issue arises when navigating to a different component and then returning to open the component with the modal as a child component, triggering the show method again. This results in multiple instances of the modal elements accumulating in the DOM without being removed.

To address this, one workaround I attempted involved incorporating a condition to add the element to the DOM and trigger the show only if the condition evaluates to true.

    <field-form-modal
        v-if="showModal"
        .....
    />

    watch: {
    showModal(show) {
        if (show) {
            UIkit.modal('#field-form-modal').show();
        }
    }
},

However, at the point when reaching UIkit.modal('#form-field-modal').show();, the element has not yet been added to the DOM. Consequently, an error occurs - Cannot read property 'show' of undefined. It seems like my assumption is accurate, as the element is only added after the showModal watch function executes.

If you have any suggestions on how to resolve this issue, I would greatly appreciate it! Thank you!

Answer №1

Ensure the parent component ID is specified as the container in the modal

// Sample Parent Component
<div id="parent-component">
    
  <sample-modal/>
</div>


// Modal Component
<div
    id="sample-modal"
    class="uk-modal"
    container="#parent-component"
>
     // contents
</div>


    

Answer №2

After implementing a workaround, I utilized the $nextTick method to ensure that the modal element is present in the DOM before calling the show function.

watch: {
    showModal: {
        handler: function(show) {
            this.$nextTick(() => {
                if (show) {
                    UIkit.modal('#field-form-modal').show();
                }
            });
        },
        deep: true
    }
}

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

I need to access the link_id value from this specific actionid and then execute the corresponding function within the Ionic framework

I have a JavaScript code in my TypeScript file. It retrieves the attribute from a span element when it is clicked. I want to store this attribute's value in a TypeScript variable and then call a TypeScript function. Take a look at my ngOnInit method, ...

Building a promotional widget

I'm currently working on developing an ad widget that can be easily reused. I'm debating whether to use jQuery or stick with pure JavaScript for this project. What are your thoughts on the best approach for creating a versatile and efficient ad w ...

The Foundation 6 Zurb Template is not compatible for offline use

After successfully installing Foundation 6 Zurb Template via the cli, I encountered no issues. I then added the missing babel install and everything worked fine online. However, BrowserSync does not seem to work offline. Upon initiating watch, I receive a ...

What causes the .getJSON function to return a MIME type error when trying to access the API

I've been attempting to make a call to the Forismatic API, but I keep encountering a MIME type error when sending it. JQuery Request: $(document).ready(function() { $("#quote-button").on("click", function(){ $.getJSON("https://api.forism ...

Prevent memory leakage by utilizing Angular's $interval feature in countdown processes

I have a controller set up to handle a countdown feature: var addzero; addzero = function(number) { if (number < 10) { return '0' + number; } return number; }; angular.module('theapp').controller('TematicCountdownCo ...

The issue of double submission persists, with the prevention of the default action

I'm in need of assistance. Within my form, I have two buttons displayed on the page: "Save" and "Publish". Below is the corresponding HTML code: <button type="submit" class="button">Save</button> <button type="button" class="button" n ...

The HTML5 Canvas ellipse with a thick line width is displaying a quirky blank space

I am currently working on a drawing application using HTML5 canvas. Users have the ability to draw ellipses and choose both line and fill colors, including transparent options. Everything works smoothly when non-transparent colors are selected. However, ...

Conceal portion in HTML until revealed

I am attempting to create 3 sections within a single HTML file using the <section id="id"> tag, and I want to be able to open each section by clicking a link in the header with <a href="#id">1</a>, and then close it when another section i ...

Retrieve JSON and HTML in an AJAX request

I have multiple pages that heavily rely on JavaScript, particularly for sorting and filtering datasets. These pages typically display a list of intricate items, usually rendered as <li> elements with HTML content. Users can delete, edit, or add item ...

Choosing the perfect item with the help of a material's GridList

I've developed a react-mobx application using Material-UI, and the code structure is similar to this: render() { // defining some constants return ( <div> <img src={selectedPhoto} alt={'image title'} /> < ...

What is the best way to link CSS files from libraries that are downloaded using npm?

For instance, let's say I installed a package: npm install --save package and it gets saved in ./node_modules/package/ Inside that folder, there might be a directory named styles and within that directory, you could find style.css. How can I link ...

I'm receiving a TypeError in Nodejs indicating that the hashPassword function is not recognized as a function. Can anyone offer advice on how to

How's everything going? I could really use your assistance! I'm currently working on developing an API for registering authenticated users, with data storage in the MongoDB Atlas database (cloud). Unfortunately, I've run into a troubling er ...

during implementation of ng-repeat directive with JSON dataset

When I receive JSON data and attempt to display it using the ng-repeat directive, I encounter an error ng-dupes error <table> <tr ng-repeat="emp in empArr"> <td>{{emp.empcode}}</td> <td>{{emp.empName}}< ...

The select() function for this.$refs.name is not available in Vue

In my application, I'm implementing Vue refs to enable selecting and copying text when a button is clicked. The code structure looks something like this: //In template <input type="text" ref="url" value="my url"/> <button @click="copylink"&g ...

Populate the Vue.js2 canvas graph with dynamically assigned array values

I am attempting to fill a canvas graph with random array values. Utilizing the Vuejs2 library, I have implemented the template for displaying the graph data as shown in the code below: import {Line} from 'vue-chartjs' export default Line.extend ...

Can you provide instructions on utilizing WYSIWYG for real-time label editing?

I am currently developing an online survey creator. The structure of a Question entity is as follows: [Question] int QuestionId { get; set; } int QuestionNumber { get; set; } String QuestionText { get; set; } QuestionType QuestionType { get; } When prese ...

The positioning of the Material Ui popover is incorrect

I am currently working on a web application project with React and have implemented Material UI for the navbar. On mobile devices, there is a 'show more' icon on the right side. However, when I click on it, the popover opens on the left side disp ...

Bringing in a variable from a component that is dynamically imported

I have a dynamically imported (Map) component on my page. const Map = dynamic(() => import('../components/Mapcomp'), { ssr: false, }) In addition to the component, I also need to import a variable from it. const [taskImg, setTaskImg] = useS ...

main.js vue 3 triggering a pre-create event

I am currently utilizing beforeCreate in the file main.js. Can anyone provide information on how to achieve a similar functionality in Vue 3? let app = Vue.createApp({ template: '<App/>', beforeCreate() { this.$store.commit('initia ...

Tips on setting up and managing configuration and registering tasks in Grunt

I've been working on a project that involves using grunt to process my Js and SASS files. The issue I'm facing is that every time I need to make a change, I have to run all the tasks in my gruntfile.js, even if it's just for one module or th ...