Unexpected behavior: Vue.js mounted method being triggered twice

I'm puzzled by the fact that the method this.fillForm() in my Vue component C (EditComment) seems to be getting called twice. Despite trying to use uuid, I am unsure of its relevance given that beforeCreate is also being triggered twice.

https://i.sstatic.net/isE9M.png

The situation involves 3 components, and here are the key segments:

Component A:

showCommentDialog: function(recordNumber) {
  this.$modal.show(
    ShowComment,
    {
      commentRecId: recordNumber
    },
    {
      draggable: true,
      width: 400,
      height: 250
    },
    {
      closed: function(event) {}
    }
  );

Component B:

<EditComment v-bind:comment-rec-id="commentRecId" v-if="showEdit"></EditComment>
  </div>
</template>

<script>
import * as $ from "jquery";
import EditComment from "./EditComment.vue";


export default {
  props: ["commentRecId"],
  data: function() {

accompanied by this functionality

editItem: function(){
      this.showEdit = true;
      console.log("editItem function() called!");
      var playerID = this.$store.state.selectedPlayer.ID;

      this.$modal.show(
              EditComment,
              {
                text: playerID
              },
              {
                draggable: true,
                width: 400,
                height: 400
              })
    }

Component C:

<script>
    import * as $ from "jquery";
    import DatePicker from "vue2-datepicker";

    let uuid = 0;

    export default {
        props: ["text", "commentRecId"],
        beforeCreate() {
            this.uuid = uuid.toString();
            uuid += 1;
            console.log("beforeCreate() uuid: " + this.uuid);
        },
        components: { DatePicker },
            data: function() {
                return {
                    commentData: {
                        comment: "",
                        customDate: ""
                    },
                    selectedCategory: "",
                    lang: {
                        default: "en"
                    },
                }
            },
            mounted: function() {
                // console.log("this._uid: " + this._uid);
                this.fillForm();
            },
        methods: {
            fillForm: function(){

Your assistance on this matter would be greatly appreciated.

Answer №1

After carefully analyzing your issue, it seems that the problem lies in how you are initiating component C within the editItem method:

this.$modal.show(
   EditComment,
   {
      text: playerID
   },
   {
      draggable: true,
      width: 400,
      height: 400
   })

If my understanding is correct, there is an error in your approach:

When using v-if, Vue triggers the component again and resets all previously passed values such as props and data properties (except for uuid as it's not a data property)

Therefore, by calling your component twice in the method with:

this.showEdit = true;

To resolve this issue, I recommend trying the following steps:

  1. Replace "v-if" with "v-show"
  2. Then display your component using this.$modal.show()

I hope this solution works for you.

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

Javascript Snake: I am having trouble making my snake's tail grow longer

I've created a snake game that is almost fully functional. The issue I'm facing is that when the snake eats food, the food changes location but the tail of the snake doesn't grow in size. I suspect that my understanding of arrays might be la ...

Ways to adjust the quantity of a product in PHP MySQL or JavaScript, including methods for increasing, decreasing, and inserting a quantity value with a checkbox option

Is it possible to adjust or remove the quantity of a product using PHP MySQL or Javascript? Can you also include a checkbox for setting a quantity value along with a product? <form action="addtocart.php" method="post"> <table width="100%" c ...

Bundle bundling all its dependencies with Webpack

Exploring webpack for the first time in my Vue project created using the vue cli. After examining the webpack bundle (produced in production mode with vue-cli-service build) through webpack-bundle-analyzer, I noticed that the file bn.js is included multipl ...

Reading and writing to a file in a UI automation using JavaScript: Tips and tricks

While running, I need to identify several properties and create a json object that I want to write to a ".json" file and save on disk. var target = UIATarget.localTarget(); var properties = new Object(); var jsonObjectToRecord = {"properties":properties} ...

Enhance your browsing experience with Javascript autocomplete featuring multi-column layouts and organized suggestions

Looking for a JavaScript autocomplete feature that can display categorized suggestions in a multicolumn layout similar to the one found on . Preferably, a jquery or YUI plugin would be ideal. ...

Tips for optimizing data retrieval from a MongoDB collection by leveraging Node.js, Mongoose, and MongoDB connections efficiently

In my scenario, I have two key collections: orders and driverResponse. The driver has the ability to either accept or decline an order, with his response being saved in the driverResponse collection. When the driver revisits the order, it is important to ...

Tips for displaying a slideshow within a div using JavaScript

When the HTML loads for the first time, the div slideshow remains hidden until a button is clicked. Any suggestions on how to display the first div without requiring a button click? var slideIndex = 1; showDivs(slideIndex); function plusDivs(n) { s ...

The modifications made to the input type="time" in View do not get updated in the Model within Angular

I have been attempting to validate the input type "time", but I am encountering an issue where changes in the view are not reflected in the model if any of the input fields are left empty. For example: https://i.sstatic.net/1U0sO.png When a user change ...

Implementing modifications to all HTML elements simultaneously

In my HTML document, there are around 80 small boxes arranged in a grid layout. Each box contains unique text but follows the same structure with three values: name, email, and mobile number. I need to switch the positions of the email and mobile number v ...

Guide on transferring morph targets from Blender to three.js?

Is there a way to export morph targets from a Blender file to three.js? I'm aiming to create a json file containing an array called "morphTargets." Any advice on how to achieve this? ...

jQuery issue: Inability of "Read More" span to reappear after clicking "Read Less"

Currently in the process of creating a portfolio website that showcases project descriptions with an excerpt, revealing full details upon clicking "Read More." My experience with jQuery is limited, but I managed to implement functionality where clicking "R ...

"Troubleshooting: How to Fix Issues with document.getElementById on Dynamic Div

Struggling to incorporate div elements and generate graphs with Google charts? The issue arises in the draw function, where attempts to access a div element using document.getElementById() result in null values and an error message stating "container not ...

JavaScript-generated div not recognizing CSS in Internet Explorer

Once again, dealing with Internet Explorer has become a major headache. On headset.no, we have incorporated a small blue search field. However, when you input "jabra" into the field, it should generate suggestions in a div underneath. This feature operates ...

Is it possible to use window.print() to print the entire contents of a DIV with both horizontal and vertical scroll bars?

In my code, I have a div that contains both horizontal and vertical scrollers. Whenever I try to print the content inside this div using the window.print() method, it only prints the visible portion of the div. Here is the code snippet that I have attempte ...

What is the best way to send an array of objects to a Node.js server?

What is the method for sending an array of objects with user data to the server for verification? I am attempting to pass orderform data to a server for validation and then display it on a different page after redirection. const addProductsToServer = (ev ...

Using an array input in a Vue.js v-for loop

Creating a form to collect information about multiple persons can be challenging. Let's say we have 3 people to gather info on, and we need to structure the data in JSON format like this: { persons[0].surname: '', persons[0].name: &apos ...

What is the process of obtaining a `csrftoken` within Vue.js?

How can I retrieve the csrftoken in a Vue.js project? I attempted to use js-cookie, but was unable to retrieve it: import Cookies from 'js-cookie'; if (Cookies.get('csrftoken') !== undefined) { // This section is skipped because Co ...

The requested `GLIBC_2.28' version required by node cannot be located, leading to an inability to amplify the version

While there were numerous questions about this topic, I am specifically seeking solutions for amplify. Below are the logs from my amplify build: 2024-01-14T16:14:17.626Z [INFO]: # Cloning repository: <a href="/cdn-cgi/l/email-protection" class="__cf_em ...

Using Vue.js to dynamically change attribute values based on a condition

I am currently displaying a v-tippy in the following manner: <label v-tippy content="My content text"> Everything is functioning as expected, but now I want to display it based on a condition show_tip == true. Is there a way to achieve th ...

Updating boolean values in Vue3 swiftly

Could someone please advise me on how to properly implement the "doneChange" method so that I can toggle the boolean value of the 'finish' property at a specific index in the tasks list? Here is my code: <template> <div> <h1>Al ...