VueJS - Building a Form Template Within a Modal Component

Struggling to include a template in a modal and unsure how to pass variables to the child template:

Below is the main HTML for the application:

<div id="example" class="container">
  <button
    class="btn btn-primary"
    type="button"
    @click="showModal = !showModal"
    @keyup.esc="showModal = false"
  >Show modal</button>

  <!-- Modal-->
  <transition
    @enter="startTransitionModal"
    @after-enter="endTransitionModal"
    @before-leave="endTransitionModal"
    @after-leave="startTransitionModal"
  >
    <div class="modal fade" v-if="showModal" ref="modal" @click.self="showModal = false">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Select Movie</h5>
            <button class="close" type="button" @click="showModal = false">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">
            <movie-form></movie-form>
          </div>
        </div>
      </div>
    </div>
  </transition>
  <div class="modal-backdrop fade d-none" ref="backdrop"></div>
</div>

Also, here is the corresponding JavaScript:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>

<script>
Vue.component('movie-form', {
  props: ['errors', 'movie'],
  template: 
  `<div>
    <form
      id="modal-form"
      @submit="checkForm"
      action="."
      method="post"
    >
      <ul v-if="errors.length">
        <li v-for="error in errors">
          {{ error }}
        </li>
      </ul>

      <div class="form-group">
        <label for="exampleFormControlSelect1">Select movie/label>
        <select
          class="form-control"
          id="movie"
          v-model="movie"
          name="movie"
        >
          <option>Star Wars</option>
          <option>Vanilla Sky</option>
          <option>Atomic Blonde</option>
        </select>
      </div>

      <div class="modal-footer">
        <button class="btn btn-secondary" @click="showModal = false">Close</button>
        <input
          class="btn btn-primary"
          type="submit"
          value="Submit"
        >
      </div>
    </form>
  </div>`,
  methods: {
    checkForm: function(e) {
      if (this.movie && this.desc) {
        return true
      }

      this.errors = []

      if (!this.movie) {
        this.errors.push('Movie is required.')
      }

      e.preventDefault()
    },
  },
})

var vm = new Vue({
  el: '#example',
  data: {
    showModal: false,
    errors: [],
    movie: '',
  },
  methods: {
    startTransitionModal() {
      vm.$refs.backdrop.classList.toggle('d-block')
      vm.$refs.modal.classList.toggle('d-block')
    },
    endTransitionModal() {
      vm.$refs.backdrop.classList.toggle('show')
      vm.$refs.modal.classList.toggle('show')
    },
  },
})
</script>

Encountering a TypeError: "errors is undefined" error when opening the modal. How can this be resolved?

Answer №1

Replace

<movie-form></movie-form>
with
<movie-form :errors="errors" :movie="movie"></movie-form>
in order to pass props from the parent component to the child component.

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

The supertest request body cannot be found

Testing my express server POST endpoint using supertest has been a challenge for me. Although everything works perfectly in postman, I encountered an issue when trying to pass body parameters into the test. It seems like the body parameters are not being p ...

What is the method to display just the final 3 characters within a paragraph?

Is there a way to display only the last three characters of a string using either a method or a string method? I consistently have a 13-digit number, but I specifically require showing only the final three digits. Appreciate any assistance provided. Than ...

The functionality of a Google chart is determined by the value of the AngularJS $scope

I recently started working with AngularJS and Google charts. I've successfully created a Google chart using data from AngularJS. It's functioning properly, but now I want to make the chart dynamic based on my Angular Scope value. I have a filter ...

Issue: 'node' is not being recognized when attempting to execute the file using the package.json script

Currently diving into the world of Node.js, I encountered an issue stating "node is not recognized as an internal or external command" whenever I attempt to start my project using either npm start or npm run start. Strangely enough, running node index.js ...

What is the reason for the excessive width of the final column in this table?

I am currently working with a dataset that I am displaying using the handsontable library in the form of a table. One issue I am facing is that the last column of my table appears too wide even though I did not specify any width for it. Below you can see t ...

How to clear a particular select option in react-bootstrap

I am looking for a solution to clear the second select when the first one selects option B. The clearing should involve removing the value, disabling it, and setting the default option to Select.... import React, { useState } from "react"; import ...

React-Tooltip trimming

Currently, I am facing a dilemma that requires some resolution. The issue at hand is related to the placement of React-Tooltip within the List element. Whenever it's positioned there, it gets clipped. On the other hand, placing it at the bottom isn&a ...

Error message: The function Vue.use is not being caught as a TypeError

Recently, I've started learning VueJS and now I'm attempting to swap out the <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89fffcecc9bba7bf">[email protected] ...

Responsive frame structure

I have set up a scene using A-frame () where I currently have a green box attached to the camera at all times. However, as the camera moves, the box also moves along with it. My question is, how can I position the box in the top right corner of the screen ...

In the world of Knockout JS, forget about using e.stopPropagation() because it won't

In my table, I have two actions that can be performed: Click event on the row level and click while checking a checkbox inside that row. However, when the checkbox is checked, I do not want the click event on the row to be triggered. <tbody data-bin ...

What causes the element to remain visible despite the changes made to the v-show attribute?

<!DOCTYPE html> <html> <head> <title>test</title> <script src="https://unpkg.com/vue"></script> </head> <body> <div id="app"> <p v-show="show&qu ...

Changing HTML dynamically does not trigger the ng-click event within ng-bind-html

I have developed a custom directive that can display messages along with rendering HTML content that may contain Angular attributes like buttons, anchor tags with ng-click attribute, and more. index.html: <ir-error-panel status="status"></ir-err ...

What steps should I take to pinpoint and resolve this duplicate error in Vue.js 2?

My project is constantly triggering an alert with a duplicated key error and a TypeError: Cannot read properties of undefined (reading 'form') alert. I am unsure how to resolve this issue or change my keys. Can someone assist me? enter code here` ...

Mastering the Art of Accelerating getJSON Array Data

Currently, I am facing a challenge with retrieving a large array (4MB) of data from the server side. I have been utilizing the jQuery getJSON method to obtain the array data and display it on the browser. However, this process has proven to be quite slow ...

Receive characteristics in component specification

Is there a way to pass the native attributes of an img element to a custom image component without explicitly defining each one, like alt, and have them all be applied to the img tag? Here is an example of what I mean: @Component({ selector: 'image ...

What is the process for retrieving a value from JSON utilizing JavaScript?

Unfortunately, I am completely stumped when it comes to Javascript. I'm attempting a few solutions based on online resources. If anyone could offer some assistance, that would be greatly appreciated. Below is the JSON data provided. It has been conde ...

Tips for implementing a dynamic value in the ng-style based on certain conditions

Is there a way to set a background-color conditionally using ng-style when the color value can be updated from $scope? These are the variables in my scope: $scope.someone = { id: '1', name: 'John', color: '#42a1cd' }; $scope ...

Sending an array of objects in JavaScript to a controller

I have a dynamic form that I'm trying to submit to my Controller. Instead of sending just a string or an array with data, I need to pass an array of objects using ajax request after building the Javascript Array. The challenge is that when I send only ...

Unable to Define Headers in Fetch GET Call

My current struggle involves sending a GET Request from the browser to my backend (which uses node + express). However, I am encountering issues with setting the headers properly. Below is the code snippet from the frontend: let accessToken = localStorage ...

Element UI defaults to using the default theme

Recently, I've been working on a Vue project set up with webpack. I'm interested in incorporating element UI into my UI library. So, I went ahead and ran the command below in my terminal: npm i element-ui -S After that, I added the following ...