What is the most effective way to showcase a list of image URLs in HTML with Vue?

Currently, I am working with an array called thumbnails that holds the paths to various images. My goal is to use masonry in Vue to arrange these images in a grid format, but I'm encountering some difficulties achieving the desired outcome.

This is my initial approach:

<ul v-for="thumbnail in thumbnails" class="masonry">
    <li v-html="thumbnail" class="masonry-brick"></li>
</ul>

However, the compilation results in this structure:

<ul class="masonry">
    <li class="masonry-brick">
        <img src="images/thumbnail1.jpg">
    </li>
</ul>
<ul class="masonry">
    <li class="masonry-brick">
        <img src="images/thumbnail2.jpg">
    </li>
</ul>

whereas what I actually want to achieve is:

<ul class="masonry">
    <li class="masonry-brick">
        <img src="images/thumbnail1.jpg">
    </li>
    <li class="masonry-brick">
        <img src="images/thumbnail2.jpg">
    </li>
</ul>

Additionally, I am considering whether eliminating the <img> tag altogether and setting the background-image of the <li> elements would be a better approach – however, I am unsure about the most effective way to implement this.

Answer №1

To repeat the element, utilize the v-for attribute on the li instead of the ul.

Iterate through the li elements like so:

<ul class="masonry">
    <li
       v-for="(thumbnail, index) in thumbnails"
       :key="index"
       v-html="thumbnail"
       class="masonry-brick"
    >
    </li>
</ul>

Refer to the official documentation for information about displaying images.

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

Removing duplicate elements from an array using lodash

What is the best way to remove duplicate values from an array? var numbers = [1, 1, 5, 5, 4, 9]; I want my result to be: var numbers = [4, 9]; Is there a method in lodash that can help me achieve this? ...

Changing the context results in the entire component being re-rendered

Currently, I am encountering a challenge where I have two different components within my Layout.js component that need to "share" props with each other. To address this issue, I introduced a ContextProvider named IntegrationProvider. However, a new proble ...

Extract information from a string to retrieve the array specifications

After making a request to the server, I received the following response: { "statusCode": 200, "body": "{\"Errors\":\"\",\"Message\":\"\",\"Output\":\"\",\"TokenID\":\"F10645774 ...

Code in Javascript is functioning properly when executed directly in the console, however, it fails to run as

I need help troubleshooting my JavaScript code that is meant to run on my homepage when it loads: $(".mm-page").css({"position":"inherit"}); I included this code at the bottom of my home.html.erb file: <% content_for(:after_js) do %> <scrip ...

Implementing a django like feature using ajax requests

I am currently working on a template where I have a link for my article: <div id="voteUp"> <h4><a href="{% url "vote_up" article.id %}">Like {{article.up_vote}}</a></h4> </div> My goal is to increase the vote count for ...

Having issues with Cypress testing of Material-UI datepicker on Github actions

Encountering an unusual issue while running Cypress tests in a GitHub action environment. The MUI datepicker is stuck in readonly mode, preventing any input of dates (works fine in other setups). Error displayed by Cypress CypressError: Timed out retryin ...

Adjustable height and maximum height with overflow functionality

Currently, I am in the process of developing a task manager for my application and facing an obstacle when trying to calculate the height of a widget. My goal is to determine the maximum height (assuming a minimum height is already set) by subtracting a ce ...

What is the best way to retrieve user input from a form within an Ajax.ActionLink?

My webpage is designed to be simple, featuring a picture and a comment section that functions as a partial view. I am looking to only refresh the partial view when a new comment is submitted. Here's what I have coded: <tr> & ...

Loading JavaScript variable with pre-processed JavaScript information

I have been working on loading test data into a javascript object and then passing it to my heating timers. While I have managed to make it work by individually inserting the code into each timer, I am looking to optimize my code and enhance my understandi ...

Issues arising from using async/await in conjunction with .then() in vue.js login function, causing fetch process not to wait for completion

I implemented a login function to verify credentials on my backend server, but I am facing an issue with waiting for the server response. Despite following the es7-async-await.js guide and trying various async/await and promise techniques, the function sti ...

Minimize the length of the styled-component class name in the upcoming iteration

Dealing with styled-components in Next along with React can pose a challenge when it comes to ensuring proper rendering of the styled components. To tackle this issue, Next offers the compiler.styledComponents flag within the next.config.js file like so: c ...

What is the best way to generate a dynamic number of div elements in Vue based on data?

This script is currently only creating a single div element with the number 4 inside it. If you want to dynamically create multiple div elements based on the value stored in the data, you need to adjust your template like this: <template> <di ...

Having trouble getting an AjaxBeginForm to function properly within a JQuery script on a PartialView

Within the main controller view, we bring in the partial view utilizing the following code: @Html.Partial("MapPartial", Model) Within the Partial View, I aim to display a map. Currently, there is an Ajax beginform with a submit button that ...

What is the best way to locate the closing tag for a specific div element?

My HTML code looks like this: <div class="body"> <h1>ddd</h1> <p>dadfsaf</p> <div id="name"> <div class="des">psd</div> <div>www</div> </div> <div&g ...

Iterate through a local storage object using JavaScript's looping mechanism

I am currently working on a project to create a shopping cart using local storage. I have initialized the local storage with the following code: var cart = {}; cart.products = []; localStorage.setItem('cart', JSON.stringify(cart)); I then use ...

Steps to eliminate the select all checkbox from mui data grid header

Is there a way to remove the Select All checkbox that appears at the top of the checkbox data column in my table? checkboxSelection The checkboxSelection feature adds checkboxes for every row, including the Select All checkbox in the header. How can I ...

Can you explain the concept of peer dependencies and plugins to me?

After reading numerous articles and posts on the topic of peer dependencies, I still find myself struggling to fully understand the concept. For instance, if coffee 1.0 has a dependency on milk 1.0, then logically coffee 1.0 would be included in my packa ...

js TouchEvent: When performing a pinch gesture with two fingers and lifting one of them up, how can you determine which finger was lifted?

I am currently working on a challenging touching gesture and have encountered the following issue: let cachedStartTouches: TouchList; let cachedMoveTouches: TouchList; function onStart(ev: TouchEvent) { // length equals 2 when two fingers pinch start ...

Error in Material UI: Cannot redeclare identifier 'Switch' - parsing issue

I am trying to incorporate the Material UI Switch component alongside the react-router-dom Switch in a single component. This is how I have included them in my react component: import { BrowserRouter as Router, Switch, Route } from "react-router-dom ...

Experiencing difficulties integrating relational data with Angular and MongoDB

I have a view where I display 'Transporters'. Each Transporter has multiple 'Deliveries', so I want to associate Deliveries with the corresponding Transporters. My tech stack includes express, mongoose, and angular.js. Here are my mode ...