Error message: The property or method "Name" is not defined on this object, but it is being referenced during the rendering process

Currently facing a challenge in implementing the modal closure functionality within the Vue.js template.

Take a look at the code snippet:

var modalInstance = new Vue({
    el: "#modalInstance",
    data: {
        displayModal: false
    }
});

Vue.component("myCustomModal",
{
    template: `
      <button class="button" @click="showModal=false">Close</button>
    `
});

export default modalInstance;

Check out the corresponding HTML:

<div id="modalInstance">
        <myCustomModal v-show="displayModal"></myCustomModal>
        <button @click="displayModal = true" class="button is-info">Open Modal</button>
</div>

Essentially, the modal opening is functioning as expected. However, the issue lies in closing the modal triggered within the template in the .js file. It generates an error message stating:

Property or method "displayModal" is not defined on the instance but referenced during render. Ensure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

Answer №1

Give this a try:

let example = new Vue({
    el: "#example",
    data: {
        displayModal: false
    }
});

Vue.component("mymodal",
{
    template: `
      <button class="button" @click="closeModal()">Cancel</button>
    `,
    methods : {
        closeModal() {
            this.$emit('closeModal');
        }
    }
});

export default example;

Add this to your HTML:

<div id="example">
        <mymodal v-show="displayModal" v-on:close-modal="displayModal = false"></mymodal>
        <button @click="displayModal = true" class="button is-info">Open Modal</button>
</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

Can Conditional Compilation be Implemented in a Vue.js Project?

When it comes to Vue.js projects, is there a way to differentiate builds for different environments using similar "flags" as in Swift/iOS development? For instance: #if STAGING // one set of logic here #endif #if PRODUCTION // another set of logic he ...

Navigating programmatically to another page in Next.js can be easily achieved by utilizing the useRouter hook

Following a post request to an external API, my goal is to navigate back to the homepage. While I am familiar with React, this is my first experience using Next.js. Here's the snippet of code: export default function New({genres}) { const createMovie ...

Efficiently Loading AJAX URLs using jQuery in Firefox

setInterval(function(){ if(current_url == ''){ window.location.hash = '#!/home'; current_url = window.location.hash.href; } else if(current_url !== window.location){ change_page(window.location.hash.split('#!/&apo ...

Can you help me understand how to access data from a selected option?

Is there a way to ensure that selecting black from the dropdown will trigger the reading of the src attribute? What modifications are needed in this code? $('.select').click(function() { var lalala = $(this).val(); $("#gallery .imgsx"). ...

Activate the button when a checkbox is ticked or when using the "select all" checkbox

I'm facing an issue with a script that should enable a button when at least one checkbox is selected and a checkbox for selecting all checkboxes is used. The problem arises when I select the "select all" checkbox as it activates the button, but if I ...

What is the best way to update the src of an input using an onclick event?

When I click the input, my background changes color as expected. However, the src of the input only changes on the second click. How can I make it change the source on the first click? Also, how can I change the src back to the original with a second cli ...

Images are failing to load in Ionic 3

Currently working on developing an Ionic application and troubleshooting the use of the camera native plugin. The script functions flawlessly in a fresh project, but encounters issues within the current project environment. Initially suspected a problem w ...

Increase the bottom padding or add some extra space to the Bootstrap form or page

I am currently working on enhancing a Bootstrap Form that includes reset/submit buttons positioned at the bottom. A common issue is when iPhone users attempt to click the Submit button, which initially displays Safari icons before requiring an extra tap to ...

Battle of Kingdoms API ajax

When attempting to access Clash of Clans API information in this script, the following error is encountered: Refused to execute script from 'https://api.clashofclans.com/v1/leagues?authorization=Bearer%20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6Ij ...

Shifting JSON Arrays in JavaScript - Changing Order with Ease

Consider the following JSON data: [ { "name": "Lily", "value": 50 }, { "name": "Sophia", "value": 500 }, { "name": "Ethan", "value": 75 } ] I am looking to verify and organize it in ...

execute function when loading a page on nuxt framework

Can Nuxt middleware be modified to execute after a page has been mounted? If the following middleware is used: export default function ({ app }) { if (!process.server) { app.$somePluginFunction() } } It currently triggers when navigating to a pag ...

The pre-loader is hidden behind the block content and navigation bar

I've encountered an issue with my preloader where it's loading in front of the <body> element but not in front of the site's <navbar> or the {% block content %}. The CSS snippet to increase the z-index of the preloader is as foll ...

Having trouble selecting a subset of data from an AJAX request using Jquery?

I have successfully implemented a modal for form submission. However, I am encountering an issue with replacing a part of the modal with a section of the data retrieved from the ajax call. $.ajax({ url: actionUrl, method: 'post', data: dataToSen ...

Unable to receive Ajax response

My current programming setup involves using a combination of JavaScript and PHP to implement an Ajax code. However, at this point in time, the outcome is showing up as undefined. To circumvent this issue, I have resorted to utilizing localStorage to trans ...

The useCallback hooks persist outdated values when the list is refreshed

Why am I not getting the expected values every time the function onRefresh is called using Hooks? Example when onRefresh is called twice: Expected values: true 0 20 false true 0 20 false Received values: false 0 0 false false 20 20 false Initial st ...

Having issues with incorporating a component into another component in VueJS

Having spent approximately 30 hours on diving into VueJS, I am encountering some difficulties when it comes to using a component within another component. Seeking assistance from someone knowledgeable in this area to provide me with some clarification. Pr ...

Morris.js tutorial: Enhancing bar charts with data labels

I have this: https://i.sstatic.net/GXjur.png But I want this instead: https://i.sstatic.net/spcS2.png Does morris.js support this feature? If not, what would be the most effective method to implement it? ...

Run code once all ajax requests have been completed

Here's the code snippet I'm working with: function updateCharts() { for (var i = 0; i < charts.length; i++) { updateChart(charts[i]); } sortQueues(); } function updateChart(chart) { $.ajax({ type: "POST", ...

An array becomes undefined once the previous array is removed

Consider the following code snippet: using the splice method, a specific item from Array1 is retrieved and stored in a variable called Popped. Next, Popped is added to array2. However, if we then delete the value from Popped, why does array2 become undef ...

Error: The term "Worker" is undefined in a new Nextjs project

I'm currently looking into an issue where I am attempting to import a webpacked javascript file into a NextJS project that utilizes Worker, but I keep encountering the error message ReferenceError: Worker is not defined. I've simplified it down t ...