My Vue.js modal component isn't functioning as expected

I have a Vue JS modal component that is supposed to display all the names typed in the form, but it isn't working. As a newcomer to Vue JS, I'm having trouble understanding why it's not functioning properly. Additionally, I am unable to use shorthands. Can anyone offer some guidance on how to troubleshoot this issue? Thank you in advance. Here is the code snippet:

<template>
  <div class="template_class">
  <div>
  <b-btn v-b-modal.modal1>Launch demo modal</b-btn>

  <!-- Main UI -->
  <div class="mt-3 mb-3">
    Submitted Names:
    <ul>
      <li v-for="n in names">{{n}}</li>
    </ul>
  </div>

  <!-- Modal Component -->
  <b-modal id="modal1" title="Submit your name" @ok="submit" @shown="clearName">

    <form @submit.stop.prevent="submit">
      <b-form-input type="text" placeholder="Enter your name" v-model="name"></b-form-input>
    </form>

  </b-modal>
</div>  
  </div>
</template>

<script>

export default {
  data: {
    name: '',
    names: []
  },
  methods: {
    clearName() {
        this.name = '';
      },
      submit(e) {
        if (!this.name) {
          alert('Please enter your name');
          return e.cancel();
        }

        this.names.push(this.name);
        this.name = '';
      }
  }
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

Answer ā„–1

Everything is running smoothly for me. Update: I took out the @shown binding from the modal because it was causing some glitches.

new Vue({
  el: '#app',
  data: {
    name: 'Pigman',
    names: []
  },
  methods: {
    clearName() {
      this.name = "";
    },
    submit(e) {
      if (!this.name) {
        alert('Please enter your name');
        return e.cancel();
      }

      this.names.push(this.name);
      this.clearName();
    }
  }
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<!-- Include this in <head> -->
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap@next/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" />

<!-- Include this after vue.js -->
<script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="//unpkg.com/tether@latest/dist/js/tether.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>

<div id="app" class="template_class">
  <div>
    <b-btn v-b-modal.modal1>Launch demo modal</b-btn>
    Current name: {{name}}

    <!-- Main UI -->
    <div class="mt-3 mb-3">
      Submitted Names:
      <ul>
        <li v-for="n in names">{{n}}</li>
      </ul>
    </div>

    <!-- Modal Component -->
    <b-modal id="modal1" title="Submit your name" @ok="submit">

      <form @submit.stop.prevent="submit">
        <b-form-input type="text" placeholder="Enter your name" v-model="name"></b-form-input>
      </form>

    </b-modal>
  </div>
</div>

Answer ā„–2

I am having trouble understanding your code. Here is a very useful example I found on the Vue homepage that you might want to check out: https://v2.vuejs.org/v2/examples/modal.html

This code snippet is directly copied from the example, and I am only referencing it as a single file component:

<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>
</template>

<script>
export default {
  name: 'modal'
}
</script>

<style>
.modal-mask {
  position: fixed;
  z-index: 9998;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .5);
  display: table;
  transition: opacity .3s ease;
}

.modal-wrapper {
  display: table-cell;
  vertical-align: middle;
}

.modal-container {
  width: 300px;
  margin: 0px auto;
  padding: 20px 30px;
  background-color: #fff;
  border-radius: 2px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
  transition: all .3s ease;
  font-family: Helvetica, Arial, sans-serif;
}

.modal-header h3 {
  margin-top: 0;
  color: #42b983;
}

.modal-body {
  margin: 20px 0;
}

.modal-default-button {
  float: right;
}

/*
 * The following styles are automatically applied to elements with
 * transition="modal" when their visibility is toggled
 * by Vue.js.
 *
 * You can easily customize the modal transition by modifying
 * these styles.
 */

.modal-enter {
  opacity: 0;
}

.modal-leave-active {
  opacity: 0;
}

.modal-enter .modal-container,
.modal-leave-active .modal-container {
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}
</style>

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

Retrieving information from a Vue component using AJAX

One of the components in my Vue project is named "testrow". It contains a form with two selections: "description" and "parameter". I need to make sure that the "parameter" changes based on the selected 'description' value stored in my database. T ...

What is the optimal event to trigger a function when there is any modification in a text area using Javascript?

I need a function to trigger every time there is any modification in my textarea, such as characters being typed, deleted, cut, pasted, etc. Currently, I am using: onkeyup || onmousemove = function(); It appears that only onmousemove is being triggered. ...

performing an AJAX request to the controller method in a Rails application

I am currently facing an issue with making an ajax call to my controller class PatientRecordController < ApplicationController def export .... end end Within my javascript file, the code snippet is as follows: $(document).ready(function(){ ...

Encasing common functions within (function($){ }(jQuery) can help to modularize and prevent

As I was creating a global JavaScript function and made some errors along the way, I finally got it to work after doing some research. However, while searching, I came across an example using (function($){ code here }(jQuery); My question is, what exact ...

Assign the private members of the class to the arguments of the constructor

class Bar { #one #two #three #four #five #six #seven #eight #nine #ten #eleven #twelve #thirteen #fourteen #fifteen #sixteen constructor( one, two, three, four, five, six, seven, eight, ...

What could be the reason behind Cesium viewer's failure to show a model after I upload it?

My application features a 3D map of a city with an "Add building" button. Upon clicking this button, a model of a building should be inserted into the map. However, all I see is a selection marker at the intended location without the actual building appea ...

Exploring the intricacies of AngularJS: Unraveling the

Iā€™m currently diving into the world of Angular and grappling with retrieving JSON data. However, the concepts of $scope and $http are still a bit fuzzy for me. Can someone provide a clear explanation of what $scope and $http mean in this context? Below ...

What is the reason behind JSLint's preference for x === "undefined" over typeof x == "undefined"?

I'm feeling lost when it comes to JSLint. Initially, my code checked if div:jqmData("me") was undefined in this way: if ( typeof el.jqmData("me") == "undefined" ? el.not(':jqmData(panel="main")').length > 0 : el.not(':jqm ...

Typescript Error: lib.d.ts file not found

Recently, I experimented with Typescript and utilized the Set data structure in this manner: var myset = new Set<string>(); I was pleasantly surprised that there was no need for additional libraries in Typescript, and the code worked smoothly. Howe ...

Having trouble deploying Vue files correctly in conjunction with a Django backend implementation

Check out my Brain Teasers site on GitHub A unique Vue (with vue cli) and Django application deployed to Heroku. While everything functions perfectly on the local machine during development, there seems to be an issue with the static file request path for ...

Using Javascript to extract formatted text from a webpage and save it to the clipboard

I've developed a straightforward tool for employees to customize their company email signature. The tool generates text with specific styling, including bold fonts and a touch of color - nothing too extravagant. When I manually copy and paste the styl ...

How to highlight all the text within a 'pre code block' when double-clicked using JavaScript

Is there a way to make code blocks on my blog automatically selected when double-clicked without using jQuery? Here is the code I have so far: I apologize if this is a silly question, I am still learning! <script type="text/javascript" src="https://c ...

Tips for fading the text of list items when their checkbox is marked as done?

I am trying to figure out how to gray out a list item when its checkbox is checked. The code I currently have takes text input from a textbox and adds it to an unordered list when the add button is clicked. Each list item contains a checkbox within it. My ...

Issue: Entering data in the input field for each row results in the same value being copied to the qty field

Currently, I have a field where users can enter the quantity for each row. Everything was functioning properly until I decided to add another input field for the pick option. Unfortunately, now it seems that whatever is entered in one field gets duplicated ...

Provide a numerical representation of how frequently one object value is found within another object value

Account Object Example in the Accounts Array: const accounts = [ { id: "5f446f2ecfaf0310387c9603", picture: "https://api.adorable.io/avatars/75/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6b7d7a666 ...

Showing the number of times a button has been pressed

I have written some HTML code to create a button and now I am looking for guidance on how I can use Vue.js to track how many times the button has been clicked. Here is what I have so far: <div class="123"> <button id = "Abutton&q ...

MVC4: Exploring the Strategy for Handling Nested Ajax Requests

Creating a form in a web application, I implemented filtering data based on user selection using AJAX to retrieve JSON nested data from BankPlans table. However, I encountered an issue when attempting to fetch all required documents for each bankID from an ...

"Encountering an issue with AngularJS where the selected ng-model value is

I'm utilizing plain options for the select tag because I only need to display a few options when they meet a certain condition. In order to perform other operations, I require the value of the selected dropdown in the controller. However, the issue is ...

Why is my code throwing an error stating "Unable to assign value to innerHTML property of null"?

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <div class="container">Lorem ipsum</div&g ...

Generating a JavaScript array containing all elements belonging to a specific class name

As I work on my website, I am attempting to create an array from elements that have a specific class. This array should retrieve the videofile attribute value from all `a` tags with the class `videoLink`. The desired values in the final array should be: ...