Child component in Vue is not functioning as expected due to an issue with

I am attempting to modify this code snippet [A] (refer to fiddle at the bottom):

  <div class="q-uploader-files scroll">
      <q-item
        v-for="file in files"
        :key="file.name"
      >
        <q-progress :percentage="file.__progress"/>
        <div>
          {{ file.__progress }}%
        </div>

        <q-item-side v-if="file.__img" :image="file.__img.src">
        </q-item-side>
        <q-item-side right>
          <q-item-tile
            :icon="file.__doneUploading ? 'mdi-done' : 'mdi-clear'"
            :color="file.__doneUploading ? 'primary' : 'color'"
            @click.native="__remove(file)"></q-item-tile>
        </q-item-side>
      </q-item>
    </div>

and transform it into [B], which essentially represents the same code but organized within a child component. Here is the parent component:

<div class="q-uploader-files scroll">
  <my-uploader-progress
    v-for="file in files"
    :file="file"
    :key="file.name"
    :color='color'
  >
  </my-uploader-progress>
</div>

And here is the child component:

<template>
  <q-item>
    <q-progress
      :color="file.__failed ? 'negative' : 'grey'"
      :percentage="file.__progress"
    />
    <div>
      {{ file.__progress }}%
    </div>

    <q-item-side v-if="file.__img" :image="file.__img.src"/>
    <q-item-side v-else icon="mdi-file" :color="color"/>
    <q-item-main :label="file.name" :sublabel="file.__size"/>

    <q-item-side right>
      <q-item-tile
        :icon="file.__doneUploading ? 'mdi-done' : 'mdi-clear'"
        :color="file.__doneUploading ? 'primary' : 'color'"
        @click.native="__remove(file)"
      />
    </q-item-side>
  </q-item>
</template>

The file property has been set on the child component:

  props: {
    file: {
      type: File,
      required: true
    }
  },

There seems to be an issue in the parent-child code since the file __img property does not exist in the child component upon rendering in [B]. See:

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

, while it does exist in [A]:

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

What could be causing this discrepancy? There are no error messages displayed in the console.

The original code ([A]) can be found here. The File Object includes an xhr instance, as far as I understand.

Here are the code sandboxes for [A] and for [B]. Click ADD and select an image for upload - it will not upload, however [A] would display its thumbnail image, etc.; perform the same action with [B], and those images will not display.

NOTE: I noticed that the file.__img property does not appear initially in [B], but once I start editing the code in the child component..., it suddenly APPEARS. Could this be due to something asynchronous, where it becomes available after a render-refresh...?!

Answer №1

To ensure the property value reacts, you must make it reactive by using a factory function as the return.

props: {
  file: {
    type: File,
    required: true,
    default: function () {
        return {
          name: '',
          _progress: 0
        }
      }
  }
},

Answer №2

If you need a solution that works, consider assigning a key to the my-uploader-progress component depending on whether an __img property exists. This will trigger a re-render when the property is available.

<my-uploader-progress
        v-for="file in files"
        :file="file"
        :key="file.__img ? '1-' + file.name : '0-' + file.name"
        :hide-upload-progress="hideUploadProgress"
        :color='color'
>
</my-uploader-progress>

See example on CodeSandbox

Answer №3

A somewhat unconventional solution, but I believe this will resolve the issue at hand. Implement a delay in the mounted function to provide some time before the file is loaded, and then set the v-if condition to true in order to render the image element in the DOM.

<template>
  <q-item-side v-if="file_loaded" :image="file.__img.src"/>
</template>

<script>
data () {
  file_loaded: false
},
mounted() {
  setTimeout(() => {
    this.file_loaded = true
  }, 1000)
}
</script>

I tested this solution on your sandbox [B] and it appears to be functioning correctly.

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

Answer №4

Updating a property value of an object in props after the child has already been mounted may not yield the desired result. In order for the change to take effect, you would need to replace the entire object.

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

Discovering the Method to Place a File into a Concealed Input Field Using Vue

I am currently in the process of previewing an image, and I want to find a way to return the file to my form for later submission on the backend. However, I am unsure of how to achieve this. Here is the code for my component: <template> <div> ...

Need to know how to invoke a function from an http callback function that resides in a separate file? Simply use the `app.get("/", callbackFun)` method

Directory Organization: testAPI contactDetail dispMobNo.js myModule.js index.js index.js const express = require("express"); const app = express(); const port = process.env.port || 3000; const getCustNo = require("./cont ...

Is there a way in JavaScript to convert comma-separated values into an array?

I currently have a code that makes combo boxes hide or show, but I am concerned about what will happen if I add more categories. If I do add more categories, I would have to modify the code each time. My goal is to have a variable that can hold multiple v ...

Utilizing Bootstrap's nav-pills component within Vue.js 2 framework

The link to the jsfiddle is located here: https://jsfiddle.net/r6o9h6zm/2/ In my project, I have implemented bootstrap nav pills in vue js 2 to show data based on the selected tab. However, I am encountering an issue where all three rooms are being displa ...

Having issues with Webpack's proxy affecting my routing flow?

Currently, I am utilizing webpack for a project running on port 8080 with a backend on port 3000. The proxy functionality is working smoothly, as I am able to send requests to the backend and access it without any issues. However, there is a need to implem ...

Problem with finding Rails controller file in .coffee extension

I recently started learning Rails and came across a file called welcome.js.coffee in the assets javascripts directory. # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in applica ...

Encountering a Troubleshooting Issue with ArcGIS JS API 4.7 and N

Recently, I have been attempting to set up ArcGIS JS API 4.7 using npm for a Vue.JS project. However, when I execute the command npm install arcgis-js-api I encounter an error with NPM: npm ERR! code E404 npm ERR! 404 Not Found: @dojo/i18n@~0.6.0 Althou ...

Firefox unable to play video recorded in Chrome

A video captured using the react plugin called, rico345100/react-multimedia-capture, was successfully recorded and uploaded to a server via Chrome. However, when attempting to play the same video in Firefox, it does not function properly. Interestingly, vi ...

Ajax external variable

I successfully uploaded a json file via Ajax using vanilla JS. { "list": [ {"name": "Executor", "date": "19.04.2017, 12:32:57"}, ... ] } Although I am able to change the "date" value for the current one using addEventListener, my challenge no ...

The tablesorter plugin from Jquery isn't functioning properly on my dynamically loaded ajax table

I am having trouble getting Tablesorter to work with my table. I attempted to use .trigger('update') but it did not have the desired effect for me. Instead, I tried using stupidtable and it worked to some extent, but not perfectly (it did not sor ...

The command "babel-node" is not recognized as either an internal or external command - Babel 7

I'm currently working with babel version 7.6.x and I have configured the setup as follows: In the package.json file: "scripts": { "dev": "nodemon --exec babel-node bin/index.js", "start": "babel-node bin/index.js", "test": "echo \" ...

Leveraging two AJAX requests within a single function

I am working on creating an ajax function to post data that I've retrieved using another ajax function. While I have figured out how to use a callback function, I am struggling with passing the data from one function to the other. Here is what I have ...

What is holding Firestore back from advancing while Firebase Realtime Database continues to evolve?

I am currently working on a chat application using Firebase within my Vue.js project. In this setup, I need to display the user's status as either active or inactive. To achieve this, I implemented the solution provided by Firebase at https://firebase ...

Is there a way to send GET and POST requests without redirection in a Node/Express application?

Hello everyone, I am new to Node/Express and currently working on an application using Express. I have a button that I want to click in order to save my data into a MongoDB database. However, I do not want this request to be sent to a specific end-point. ...

A guide on changing a plus sign into a minus sign with CSS transition

Is there a way to create a toggle button that changes from a plus sign to a minus sign using only CSS, without the need for pseudo-elements? The desired effect is to have the vertical line in the "+" sign shrink into the horizontal line. While I know it& ...

Error: React.js - The specified element type is invalid

After setting up routing using react-router and trying to access the URL, I encountered an error message saying "Element type is invalid." Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for compos ...

Integrating Dialogflow with a Heroku JavaScript application

After extensive research, I delved into the realm of integrating DialogFlow requests with a webhook hosted on platforms like Heroku. With both Heroku and nodeJS impeccably installed on my system, I diligently followed the heroku tutorial to kickstart the p ...

The reason why CSS is not being applied to a component in one Vue app when it is integrated into another app

Hey there, I've encountered a scenario where the css and scss styles are not being applied to a component from one app when used in another app. I have two separate applications: core plugins I'm trying to utilize components from the core app ...

The functionality of Component OutlinedInput with parameter "notched" and Component InputLabel with parameter "shrim" is experiencing issues in Material UI version 5.15.2

I am facing a unique issue.. In version Material UI 5.15.2, I have created a custom component to handle the display of InputLabel and OutlinedInput based on specific conditions.. Below is an example where I provide certain parameters to guide my component: ...

The jQuery click event appears to be malfunctioning

Attempting to create a basic calculator using jQuery, the code seems straightforward but nothing is happening when executed. It's as if the JavaScript file isn't linked properly. Below is the jQuery code snippet: function addNumber(num){ ...