What is the best way to show a pop-up modal in Vue.js?

Having some trouble getting the Vue Modal to work in my Vue-cli setup for reasons unknown. Attempting to implement the modal example from: https://v2.vuejs.org/v2/examples/modal.html

I'm still fairly new to Vue, so bear with me and my basic question.

Any assistance would be greatly appreciated.

Index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body>
      <script type="text/x-template" id="modal-template">
        <transition name="modal">
          <div class="modal-mask">
            <div class="modal-wrapper">
              <div class="modal-container">
      
                <div class="modal-header">
                  <slot name="header">
                    default header
                  </slot>
                </div>
      
                <div class="modal-body">
                  <slot name="body">
                    default body
                  </slot>
                </div>
      
                <div class="modal-footer">
                  <slot name="footer">
                    default footer
                    <button class="modal-default-button" @click="$emit('close')">
                      OK
                    </button>
                  </slot>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </script>
    <main id="app"></main>
  </body>
</html>

Main.js

import Vue from 'vue'
import App from './App'
import router from './router'

Vue.component('modal', {
  template: '#modal-template'
})

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App },
  data: {
    showModal: false
  }
})

Page.vue

<template>
<div>
  <button id="show-modal" @click="showModal = true">Show Modal</button>
  <!-- use the modal component, pass in the prop -->
  <modal v-if="showModal" @close="showModal = false">
    <!--
      you can use custom content here to overwrite
      default content
    -->
    <h3 slot="header">custom header</h3>
  </modal>
</div>
</template>

<script>
  export default {
    name: 'VueModal',
    methods: {
      hideModal () {
        return this.$store.commit('hideModal')
      }
    }
  }
</script>

Answer №1

Looks like Vuex is not being utilized, so the line of code

return this.$store.commit('hideModal')

will not function as intended.

Furthermore, your showModal resides in Main.js instead of Page.vue, and the method hideModal() is not being used at all. Update your script in Page.vue as follows:

<script>
  export default {
    name: 'VueModal',
    data() {
      return { showModal: false };
    }
  }
</script>

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

Activate the dialog box exclusively after data has been submitted via the submit button on a form

My goal is to create a data saving functionality with a next button. After filling out the form, clicking on the next button triggers a popup dialog asking, "Do you want to submit your data?" To achieve this, I included the following code in my submit but ...

Python's Selenium execute script does not support setTimeout function

Hey there! I was attempting to pause the Selenium execution for a few seconds in order to wait for a Modal popup to appear. Unfortunately, using time.sleep(5) did not work with PhantomJS (apparently, PhantomJS does not support sleep function). So, I decide ...

Issue with Box2Dweb's Rope Joint functionality has been identified

I am facing an issue with the Rope Joint code in Box2dweb. Despite running the code in my browser, I am only seeing a blank canvas with no activity. However, when I remove the lines that define the joints (the eight lines following //joints), the code ru ...

The verification of form is not done using an if statement

There are two forms in my code named formA and comments that I need to submit via AJAX. However, the current if and else conditions do not correctly identify the form and always trigger the alert message hello3. Here is the JavaScript function: function ...

How to use Javascript, HTML5, and AngularJS to display and print a PDF document within a

I have a situation where I need to load a Base64 encoded PDF as a String from my server into JavaScript in my client application which is built using AngularJS and HTML5. The structure of my HTML code is as follows: <div id="printablePdfContainer"> ...

The depthFunc feature in three.js

After reviewing the code, it appears that there might not be much control over the depthFunc with three.js. Can you confirm if it is only set once as the default GL state and not modifiable within the material? I haven't come across any examples demo ...

Can terminating a POST XHR request be trusted?

Running an apache server with PHP 5.4. I've set up a form that sends an asynchronous request to the server and stops any previous requests if the button is clicked again before the current request is completed. If I repeatedly click the button quick ...

Utilizing JavaScript to sift through JSON data

Looking to efficiently parse a large JSON file based on its values. This is an excerpt from my extensive JSON code (with over 1000 entries). var jsonObject = [ { "UserId":10259, "FullName":"hello world", "CustomerId":"10165" ...

Shopping cart has encountered an issue with storing the data correctly

While I've managed to successfully integrate another service, the challenge now lies in implementing the logic for correctly generating cart items. My goal is to increment the quantity of items in the cart by one with each function call, but it seems ...

Tips for implementing a CSS loader exclusively on a particular section of your content

Is it possible to apply a loader image to a specific section of content on a webpage? All the tutorials I've come across for loader images focus on applying them to entire webpages. However, I am looking to implement a simple loader only to a specifi ...

Display the JSON data in a table only if the ID is a match

I am working on a table that consists of 5 rows and 2 columns. Each row is assigned an ID ranging from 1 to 5. My goal is to insert JSON data into the table, but only if the ID in the data matches the ID of the corresponding row. If there is no matching I ...

Cross-origin resource sharing problem encountered in Firefox and Internet Explorer, while functioning properly in Chrome

I am encountering CORS errors in Firefox and IE, but everything is functioning properly in Chrome. The two requests causing the issue are A general call to Facebook which works in Chrome and Firefox, but fails in IE 11. I am making a request to verify t ...

Invoke Javascript through CSS selector

On my webpage, I have implemented a mousemove-mask feature that was inspired by an existing codepen. However, there is one significant issue that I can't seem to resolve on my own. Despite my attempts to fix it, I believe someone with more expertise c ...

Error: Sveltestrap Tooltip encounters a ReferenceError due to process not being defined in the createPopper.js file

Looking for assistance with integrating Tooltip in Svelte using SvelteStrap. Below is a simple example: <script> import { Button, Tooltip, } from 'sveltestrap'; </script> <Button id="btntest">t ...

utilizing a spacemouse in conjunction with autodesk forge

Recently, I acquired a 3dconnexion spacemouse and have been attempting to configure it to work with the forge viewer. Fortunately, I came across some JS samples in the SDK for three.js that worked flawlessly. It seems both forge and three.js utilize webgl ...

Caution: Circular dependency has been identified in Angular 10 involving barrels

I keep getting a warning about circular dependencies when using barrelsby in Angular 10. Error: WARNING in Circular dependency detected: src\app\core\components\components.module.ts -> src\app\core\components\hea ...

How come it functions with $scope but fails with `this`?

I am trying to figure out why my code only works with scripts 1 and 3, but not with script 2. I need this functionality for my project because I can't use $scope. Thank you!! <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19 ...

The Bootstrap carousel is stuck and not moving

I'm really struggling to figure out why the bootstrap carousel code isn't working for me. It's frustrating because I followed the example on the bootstrap website and only made minor customizations, so it should be functioning properly. I su ...

CSS tip: Create a trendy design by adding a slanted border to the last

I need help creating a unique menu list with a white border design. The borders should all be straight by default, except for the last border which must be slanted. ul { background-color: #183650; } li { list-style: none; display: inline-block; b ...

Is searching for duplicate entries in an array using a specific key?

Below is an array structure: [ { "Date": "2020-07", "data": [ { "id": "35ebd073-600c-4be4-a750-41c4be5ed24a", "Date": "2020-07-03T00:00:00.000Z", ...