Having trouble with Vue.js data binding for image sources?

I am having an issue with a component that reads data from IndexedDB (ArrayBuffer) and uses it as the source for an image. The problem is that when the parent component utilizes this component, only the last formUpload has its dataSource set. Even though my console.log statements show that the data is present and the URL.createObjectURL is successfully created, the images are not being assigned as the source of the image. Any suggestions?

<div class="ui grid segment" v-if="formData.Uploads.length > 0">
            <div class="four wide column" v-for="upload in formData.Uploads" style="position:relative;">
                <formUpload :upload="upload"></formUpload>
            </div>
        </div>

        Vue.component("formUpload", {
            props: ["upload"],
            template: `
                <div class="ui fluid image">
                    <a class="ui green left corner label">
                        <i class="add icon"></i>
                    </a>
                    <a class="ui red right corner label" v-on:click="removeUpload(upload)">
                        <i class="remove icon"></i>
                    </a>
                    <img style="width:100%;" :src="dataSource" />
                    <div class="ui blue labels" v-if="upload.tags.length > 0" style="margin-top:5px;">
                        <image-tag v-for="tag in upload.tags" :tagtext="tag" :key="tag" :upload="upload.id" v-on:deletetag="deletetag"></image-tag>
                    </div>
                </div>
            `,
            data: function () {
                return {
                    dataSource: undefined
                }
            },
            mounted: function () {
                _this = this;

                imageStore.getItem(_this.upload.imageId).then(function (result) {

                    console.log("Data gotten", result.data);

                    var dataView = new DataView(result.data);
                    var blob = new Blob([dataView], { type: result.type });

                    console.log("Data transformed", blob);

                    _this.dataSource = URL.createObjectURL(blob);

                    console.log("DataUrl", _this.dataSource);

                });

            },
            methods: {
                removeUpload: function (upload) {
                    console.log("removeUpload");
                }
            }
        });
        
    

Answer №1

Simply put, the key is essential.

When updating a list of elements with v-for, Vue typically uses an "in-place patch" strategy. If the order of data items changes, Vue will patch each element in place rather than rearranging them to match the new order. This approach is similar to using track-by="$index" in Vue 1.x.

While this default mode is efficient, it's only suitable when your list rendering doesn't depend on child component state or temporary DOM state (like form input values).
[...]
It's generally recommended to use a key with v-for whenever possible, unless the iterated DOM content is straightforward or you deliberately rely on the default behavior for performance reasons.

This can be a common stumbling block, but it should be easier to handle thanks to the fact that

Starting from version 2.2.0, a key is now mandatory when using v-for with a 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

Calculating and displaying the output on an HTML page: A step-by-step guide

Within my HTML, I have two values stored in Session Storage: "Money" and "Time". These values are based on what the user inputted on a previous page. For example, if someone entered that they need to pay $100 in 2 days. My goal is to generate a list that ...

Tips for displaying a restricted quantity of items in a list according to the number of li lines used

My approach involves using a bulleted list to display a limited number of items without a scrollbar in 'UL' upon page load. On clicking a 'more' button, I aim to reveal the remaining items with a scrollbar in UL. The following code acco ...

Is it possible to define a state within a map function?

This section of my code retrieves JSON data from the backend API and displays it as a list. Here is an example of my JSON data: (15) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 0: {id: 1, t ...

Nuxt 3: Pinia: How does mutating my state affect the integrity of the entire array?

My code is here: import { defineStore } from "pinia"; export const DB_CART = defineStore("CART", { state: () => ({ CART: [ { $id: "62616ffc6d13e2a9eb04", quantity: 1 ...

Exploring ways to extract HREF using Selenium in combination with Node JS

I am having trouble extracting the hrefs from my web element using .getAttribute("href"). It works fine when applied to a single variable, but not when looping through my array. const {Builder, By, Key, until} = require('selenium-webdriver'); (a ...

Creating a 3D polygon in three.js using three vertices

I've been attempting to render a plane using a set of 3 vertices (shown below). Unfortunately, none of the methods I have tried - mostly sourced from SO or the official three.js forum - seem to be working for me. // example vertices const vert1 = new ...

Choosing choices from an array of values in a selection box

Using Ajax request with Jquery, I am receiving some values in JSON format. When I try alert(msg.options), it displays ["1","3","8"] The following script successfully selects the required options with values 1, 3, and 8: $('#input_6').val(["1"," ...

Share the Angular library distribution folder on a new git repository

I am faced with a dilemma regarding my Angular library that I often use in another project. The way I build my library is by running the command: ng build Upon executing this command, a dist/my-library directory is created at the library root. My goal is ...

Advanced jQuery Autocomplete with Nested Ajax Requests

I am currently developing a feature that allows users to search for albums using the Spotify Metadata API. The main functionality is working well, but I'm running into an issue with retrieving album cover art when making nested calls. This seems to be ...

Guide on how to navigate to a different webpage once the ajax request is complete

When making a JQuery ajax call to invoke a void method, I have encountered the need to redirect the user to the home page upon successful login. Below is an example of how this can be achieved: var options = { url: "/Account/Login", data: formvalu ...

Display logo when website has been scrolled

On my website, I want to display a logo in the header only when the site has been scrolled. I attempted to accomplish this with JavaScript: if(document.getElementById("div").scrollTop != 0){ document.write("<img src='logo.jpg'>"); } How ...

Displaying a collapsible table directly centered within the table header

I am having trouble centering my table header in the web browser page. When I click the "+" button, the data is displayed beneath the table header, but I want the collapsible table to be centered directly below the header. I have tried making changes in CS ...

What is the best way to manage photos from your phone without having to upload them online?

I'm currently developing an application using AngularJS/Javascript, HTML, and CSS within a cloud-based environment on c9.io. My next task is to create and test an app that can access the phone's photo album, apply filters to images, and I'm ...

Combining various functions into a single button

I am currently working on creating a full-screen menu that functions like a modal. Everything seems to be working fine, except for the fadeOut animation. Can someone please help me understand what is causing issues with my scripts/codes? I want the content ...

Issue with Android Post to API not resolved, however Ajax Post is functioning correctly. The necessary string parameter 'firstname' is missing

Despite having a functional Spring Framework API setup for receiving GET/POST requests on the web, I encountered an issue when attempting to post from an Android device. The error message "Required String parameter 'firstname' is not present" kep ...

error: vue-simplemde npm package does not include markdown-editor

Since upgrading vue-simplemde from npm version 0.5.2 to 2.0.1, I am unable to locate vue-simplemde/src/markdown-editor in my node_modules directory. How can I go about replacing it? ...

Create a grid layout using Bootstrap and Vue that features responsive columns which resemble a snake pattern. The alternate rows should be displayed in reverse order for a visually dynamic

Having a bit of a tricky situation here, hoping someone can provide some insight. I am currently working on a project using Laravel, Vue, and Bootstrap where I need to loop through some objects and display them in a specific pattern. 1 2 3 6 5 4 7 8 9 Th ...

I am trying to figure out the best way to position the navbar directly under the jumbotron in Bootstrap 4

Obtaining information is possible with Bootstrap 3, yet I am struggling to understand how to implement it with Bootstrap 4. ...

The ng-bootstrap modal fails to appear when incorporating [formGroup]="FormName" or formControlName="elementName"

Utilizing ng-bootstrap to create a popup modal has been a challenge for me. When I import FormsModule and ReactiveFormsModule in src/app/modal-basic.module.ts, the code inside it looks like this: import { NgModule } from '@angular/core'; import { ...

When accessing nested objects in Props in Vue.js, an error may occur if the property of null/undefined

When I make an axios call, I receive an object which I pass to a child component. However, when I attempt to loop through the object in the child component to access a property of another nested object, I encounter an issue where the property is showing as ...