"Vue is in the process of loading an image from a

Having trouble loading an image in vue.js

I've been attempting to load my image without success. If anyone has a solution, I would greatly appreciate your help.

This is the HTML source:

<div class="col-md-4 col-sm-6 portfolio-item" v-for="(obj, key) in portfolioJSON" :key="key">
          <a class="portfolio-link" data-toggle="modal" href="#portfolioModal2">
            <div class="portfolio-hover">
              <div class="portfolio-hover-content">
                <i class="fas fa-plus fa-3x"></i>
              </div>
            </div>
            <img v-for="image in imageArray" :src="image" class="img-fluid" alt="">
          </a>
          <div class="portfolio-caption">
            <h4>{{ obj.caption }}</h4>
            <p class="text-muted">{{ obj.title }}</p>
          </div>
        </div>

And here is the corresponding JavaScript code:

export default {
  computed: {
    imageArray: function() {
        return this.portfolioJSON.map(obj => obj.img)
    }
},
    data: () => ({
        portfolioJSON: [
            {
                img: '/something/random/img.jpg',
                caption: 'test',
                title: 'test1'
            },
            {
                img: '/something/random/img.jpg',
                caption: 'Explore',
                title: 'test2'
            }
        ]

    })

}

Despite following these steps, the image is not rendering as expected and only one picture is displayed.

Answer №1

Your portfolioJSON variable contains an array of objects, with the src accepting the path of a single image.

To display all images in the array, add this to your Vue instance:

computed: {
    imageArray: function() {
        return this.portfolioJSON.map(obj => obj.img)
    }
}

This code will create an array of paths based on your portfolioJSON data.

In your HTML, make this adjustment:

<img :src="portfolioJSON" class="img-fluid" alt="">

Replace it with:

<img v-for="image in imageArray" :src="image" class="img-fluid" alt=""></img>

This change will render an <img> element for each image in your portfolioJSON array (and remember to properly close tags when using Vue to avoid issues later).

If you prefer to use a single <img> element with srcset instead of src, use the following HTML snippet:

<img :srcset="imageArray.join(',')" class="img-fluid" alt=""></img>

Keep in mind that additional information can be included in the srcset along with the image sources.

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

Issue with Fat-Free Framework and Abide AJAX form submission malfunction

Currently, I am utilizing Foundation 5.5.3 and Abide form validation in order to facilitate site registration. This is reflected in my Javascript code below: $('form#register').on('valid.fndtn.abide', function() { var data = { ...

Removing an element in JSON is resulting in an element that is undefined

Dealing with a JSON Object, I am faced with a challenge where deleting elements results in undefined entries, causing issues in my usage within datatables. Here is my current approach: function dTable(presId){ var allregos = '[{"presId": "a09N0000 ...

Ways to verify the existence of empty arrays in an Object using underscore.js or jQuery

Is there a more efficient method to determine whether an object contains empty arrays (0-*) aside from the following approach: emptyArr: function() { var obj = getObj(); return obj.abc.length == 0 || obj.def.length == 0 || obj.ghi.length = ...

Having trouble terminating the session with the authentication provider SSO on Node JS

I'm struggling with ending the session properly when a user makes a request to my /logout endpoint. I want to clear the session and require the user to log in again via SSO. Currently, after an initial login, I remain logged in without needing to re-e ...

When I click on my div, the color does not change as expected

Recently, I wrote a code snippet where there are 9 different div elements each assigned a ".spaces" class. The intention was to change the color of these divs upon clicking on them. However, I encountered an issue where only the first div changes its color ...

Remove identical options from the dropdown menu

After hard-coding and adding items to the dropdown list for team size, such as 1, 2, 3, I am encountering an issue when loading it for editing or updating. Duplicate values are appearing in the list: 1 1 2 3 4... How can I remove these duplicate value ...

The content within the div appears to be in a perpetual state

I'm working on creating a chat application with a single interface. I've encountered an issue where the content in the middle div "messages" keeps bouncing, including the scroll bar. How can I resolve this problem? Additionally, I'm unable ...

What is the best way to handle responses in axios when dealing with APIs that stream data using Server-Sent Events (S

Environment: web browser, javascript. I am looking to utilize the post method to interact with a Server-Send Events (SSE) API such as: curl https://api.openai.com/v1/completions \ -H "Content-Type: application/json" \ -H ...

To ensure a rectangular image is displayed as a square, adjust its side length to match the width of the parent div dynamically

How can I make the images inside my centered flexbox parent div, #con, be a square with side length equal to the width of the parent div? The image-containing div (.block) is positioned between two text divs (#info and #links). I want the images to be squa ...

Upon refreshing the page, an inline script was not executed due to its violation of the Content Security Policy directive: "script-src 'self'"

When I refresh the page on my production build for all routes except "/", I encounter an error in my Node/React app which results in a blank page being displayed. The error message states: "Refused to execute inline script because it violates the followi ...

Exploring the wonders of Angularjs and the dynamic capabilities of streaming data

Using AngularJS's $http to request streaming data like this: $http({ method: 'POST', url: url, data: JSON.stringify(data), headers: config }).success(function(responseData) { console.log(responseData); }).error(funct ...

What is the best way to convert a 2D PHP array into a JavaScript array?

I have encountered a problem with a PHP array setup as follows: $output = array(array(1,1,1,1),array(2,2,2,2),array(3,3,3,3)); When I encoded the array to JSON, I received this output: $output = {"1":[1,1,1,1],"2":[2,2,2,2],"3":[3,3,3,3]} My goal is to ...

Guide to defining the typescript type of a component along with its properties

I am facing an issue with my SampleComponent.tsx file: import React from 'react'; type Props = { text: string }; export const SampleComponent: React.FC<Props> = ({text})=><div>{text}</div>; SampleComponent.variant1 = ({tex ...

Optimal approach for changing state in VueJs 3

While exploring the Pinia documentation, I came across a method to modify the state directly using ...mapWritableState or store.$patch(). However, I have always been taught to avoid direct state modification and instead use 'actions' in my code. ...

Experiencing issues with ng-repeat in AngularJs?

I am currently facing an issue with two tables that are rendering data through AngularJS from two separate C# methods. Both tables have almost identical structures, with the first one being used as a search field and the second one to display names. The pr ...

JS issue: Having trouble accessing the array values despite the array being present

I am working on an ajax call where I save the success data in an array. However, when I try to access that data outside of the ajax function and use console to log my array, it appears as expected. Here is a glimpse at what I see on the screen: https://i ...

A dysfunctional JS object

I am grappling with a rather intricate object and have simplified it by removing unnecessary functions. However, I am facing an issue when I try to initialize it and I can't figure out the reason behind it. Safari is throwing this error at me: [Error] ...

Determine the total quantity of colored cells within a row of an HTML table and display the count in its own

I need assistance with a table that has 32 columns. From columns 1 to 31, I am able to fill in colors by clicking on the cells. However, I now want to calculate and display the number of cells that are not colored in the last column. This task must be co ...

Generate a table containing information organized by category

I am looking to create a component Table that groups countries together. Here is my mockup: enter image description here I am struggling to find a solution for this issue. Can someone please assist me? ...

Enhancing the appearance of a JSX component in React

I have a segment of code within my project that calculates a specific percentage and displays it on the dashboard. <text className="netProfit-circle-text" x="50%" y="50%" dy=".2em" textAnchor="middl ...