Attaching Picture From Array To Vue

Is it possible for me to request assistance? I'm wondering how to bind an image to a vue component or more simply, how do you render an image from an array in vue? Allow me to share my code with you and explain in detail how I have approached this.

Within my template, I have a UL that displays 3 items from an array. My goal is to display images from the array that I have set up in my JavaScript code.

<ul class="list-rendering">
                <li v-for="item in items"  v-bind:key="item">
                    <v-card style="padding: 10px;">
                        <div class="img-cont">
                            <v-img :src="require('@/assets/section/about.jpg')"></v-img>
                        </div>
                        <div class="text-cont">
                            <br>
                            <base-subheading>{{ item.name }}</base-subheading>
                            <h5>{{ item.price }}</h5>
                        </div>
                    </v-card>
                </li>
            </ul>
export default {
data () {
    return {
        items: [
            {
                img: "@/assets/section/about.jpg",
                name: "Cuppy Cake",
                price: "$20.00"
            },
            {
                img: "@/assets/section/social.jpg",
                name: "Red Velvet Cake",
                price: "$4.99"
            },
            {
                img: "@/assets/section/testimonials.jpg",
                name: "Carrot Cake",
                price: "$3.00"
            }
        ]
    }
}

}

Answer №1

Give this a shot...

<ul class="list-formatting">
  <li v-for="element in elements"  v-bind:key="element">
    <v-card style="padding: 10px;">
        <div class="picture-container">
            <img :src="element.img" alt="loading...">
        </div>
        <div class="description-container">
            <br>
            <base-subheading>{{ element.name }}</base-subheading>
            <h5>{{ element.price }}</h5>
        </div>
    </v-card>
  </li>
</ul>

Answer №2

Give this a shot

<ul class="list-rendering">
                <li v-for="item in items"  v-bind:key="item">
                    <v-card style="padding: 10px;">
                        <div class="img-cont">
                            <v-img :src="require(item.img)"></v-img>
                        </div>
                        <div class="text-cont">
                            <br>
                            <base-subheading>{{ item.name }}</base-subheading>
                            <h5>{{ item.price }}</h5>
                        </div>
                    </v-card>
                </li>
            </ul>

To display the image with an alias, you need to use require(ITEM.IMG). If using a relative path, simply use :src="item.img"

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

Replace minor components (SVG) within the primary SVG illustration

I'm interested in transforming SVG elements into the main SVG element. For example, let's say the black square represents the main SVG element. I want to change elements 1, 2, and 3 to different SVG elements using JavaScript code. However, I am u ...

Guide on activating javascript code for form validation using php

How can I activate JavaScript code for form validation? I am currently implementing form validation on a combined login/register form where the login form is initially displayed and the register form becomes visible when a user clicks a button, triggering ...

Align Image According to Dimensions of the Browser

I'm looking for a way to dynamically position an image based on the width and height of the browser within an HTML file. I know that adjusting an image's width/height is possible through code like this: <script> ...

Retrieve the AngularJS scope object by using an alternate scope object as the identifier

As I loop through an array of person objects using ng-repeat, imagine the array looks something like this: [{ "display_name": "John Smith", "status": "part-time", "bio": "I am a person. I do people stuff.", }, { "display_name": "Jane Doe", "stat ...

Ways to create two distinct "on click" actions for a single image to execute two distinct tasks

Clicking on an image will open a slider showing all images before filtering. Once filtered, only the selected images will be displayed in the slider. <div class="gallery row" id="gallery" style="margin:0;"> <!-- Grid column --> <div ...

Shadow and Quality Issues with SVG Images

I have designed a unique SVG image with intricate details and a decorative frame, enhanced with shadowing effects. Unfortunately, after importing this SVG into a react-native application using the react-native-svg library, I noticed that the shadow around ...

Using Local Storage to store arrays in JavaScript/jQuery

Currently, I am implementing a set of multiple buttons each containing data-id and data-name Below is my concept along with some sample code for reference: $(".clickCompare").click(function ({ var id = $(this).attr('data-id'); var ...

Learn the best way to efficiently transfer multiple checkbox selections in a single object using AJAX

In my form, I have 4 checkboxes with unique IDs like filter_AFFILIATION_1, filter_AFFILIATION_2, and so on up to 4. My goal is to dynamically send the values of checked checkboxes to the server using an ajax call. Below is the snippet of my code: $(&a ...

Having trouble with loading a new page on an HTML website

My website is successfully updating the database, printing all values, but for some reason the new page is not opening. The current page just keeps refreshing and I'm receiving a status of 0. Below is my code snippet: try{ console.log("here1 "+e ...

Implement a dropdown menu for filtering, but it is currently not functioning as expected

When I select a city_name, my goal is for the graph to only display information pertaining to that particular city. In the params section of my code, I have included filtering options using a selection menu in Vega-Lite. However, despite selecting Brisba ...

Incorporating CouchDB storage into a Node.js socket.io JSON data stream

Currently in the process of researching how to implement persistence for a real-time Twitter JSON feed in Node.js. The stream is set up and sending data to the client, but I'm struggling with storing this information in a CouchDB database so that it c ...

Retrieve the names contained within TD elements using the console

I always enjoy experimenting with new things. Take a look at the https://lodash.com/docs/4.17.15 lodash documentation site where you'll find a menu on the left side featuring all available functions. Is there a way to extract the names of these functi ...

Having trouble connecting to Azure SQL server from my Node.js application

I'm attempting to run a basic query to my Azure SQL server from NodeJs, but I'm encountering errors indicating that I cannot establish a connection to the server. The details provided in my code are correct because I've used them successfull ...

Transform the object into JSON while excluding specific (private) attributes

I recently started using dean edwards base.js for organizing my program into objects. I must say, base.js is truly amazing! But now I have a question that doesn't require prior knowledge of base.js to answer. Within one of my objects, I have a proper ...

Manipulate your table data with jQuery: add, update, delete, and display information with ease

When adding data dynamically to the table and attempting to edit, the checkbox value is not updating. This issue needs to be resolved. You can view the code in action on jsFiddle here on JSFiddle ...

Just a quick inquiry regarding adding new line characters in JSON to be used in

After encountering an issue with a JSON file in my JavaScript application where it would not print new lines when viewed on the console, I am at a loss for a solution. The contents of my JSON file are as follows: [ { "id": "71046" ...

How can I achieve a smooth opacity transition without using jQuery?

Although I understand that JQuery offers functions for this purpose, I am interested in finding a solution using only pure javascript. Is there a way to change the CSS opacity setting over time? Perhaps utilizing a unix time stamp with millisecond precisi ...

Guide on setting the value of a select element in Vue Core UI

Apologies for the newbie question, as I am just starting out with Vue.js. I have recently started using CoreUI, but I am having trouble finding detailed documentation or tutorials on CoreUI+Vue integration. Specifically, I am working with the <CForm> ...

I'm encountering an error when trying to use makeStyles

Something seems off with MUI. I was working on my project yesterday and makeStyles was functioning properly, but now it's suddenly stopped working. I'm encountering an error when calling it here: I suspect the issue lies in the import statement ...

Determine in Node.js whether an image is empty or not

I am in the process of developing a testing application that generates an image based on the URL provided by the user. The screenshots are captured using phantomJS and nightmareJS. Occasionally, users may input a non-existent URL, resulting in a blank ima ...