Unable to display Vue image within v-for loop

I'm facing an issue with rendering images from an array of objects. Even though the paths to the images are correct, the images are not displaying.

I've been following a tutorial at which suggests specifying the image URLs as links. I've tried using both https and http for localhost but the images remain blank.

I also attempted to manually set the image sources using a function, but encountered the same problem. I've included the small component code snippet below:

If anyone has any suggestions on what I should try next, I would greatly appreciate it :)

<template>
  <div id="sideBarDiv">
      
        <img src="../assets/Icon_3.png" height="40px" width="40px"/>   
        <div v-for="link in links" :key="link.id">
            <p>.</p> <img v-bind:src="link.src" height="15px" width="15px"/>
        </div>
   
    </div>
    
</template>

<script>


export default {
  name: 'sidebar',
  data: function(){
      return{
          links:[
              {
                  id: 0,
                  src: 'https://src/assets/superdash/ic_active-2.png',
                  isActive: false
              },
               {
                  id: 1,
                  src: 'https://src/assets/superdash/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="18717b47799b6c716e7d582be56f9cfcad4">[email protected]</a>',
                  isActive: false
              },
              {
                  id: 2,
                  src: 'https://src/assets/superdash/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93faf0ccf2f0e7fae5f6372f378cbc5c8dcb9cdfedec2cee5">[email protected]</a>',
                  isActive: false
              },
              {
                  id: 3,
                  src: 'https://src/assets/superdash/ic_active-3.png',
                  isActive: false
              },
              {
                  id: 4,
                  src: 'https://src/assets/superdash/ic_active-4.png',
                  isActive: false
              }
          ]
      }
  },
  methods:{
       getLinkImage(img){ 
          return ('@/assets/superdash/'+img+'.png'); //attempt to plugin image name and combine with known path to render
      },
    
  }
 
}
</script>



Answer №1

After some experimentation, I managed to solve the issue. Surprisingly, the 'require' function can be used directly within the img component. Additionally, using '@/assets' also works seamlessly. Another workaround is utilizing a method, as long as the 'require' keyword is explicitly included.

<img :src="require(`../assets/superdash/${link.src}.png`)" height="40px" width="40px"/>

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

How to disable click event binding in Angular2 after it has been clicked once

Within my application, there is a button that has a click event attached to it: <button class="btn btn-default" (click)="doSomething()"> I am wondering if there is a way to remove the (click) event from the button within the doSomething method so t ...

Tips for tackling drag and drop functionality with only HTML, CSS, and JavaScript

Currently, I am faced with a challenge involving drag and drop TEST. Observing the image provided below, you will notice 3 columns each containing 3 small boxes of varying colors. These boxes are draggable, meaning they can be moved between columns follow ...

Leverage the Ajax response to bypass a loop iteration in Jquery

I am currently facing a challenge where I need to skip a loop iteration based on the response received from an Ajax call. The issue lies in the fact that I am unable to access the Ajax response outside of the actual Ajax call. While I have managed to fin ...

Steps to select an option from a drop-down menu that does not have an ID assigned

How can I interact with a drop-down element that appears after clicking on an item? <div class="field loan-selection"> <label class="field__body"> <div class="field__label">Nettokreditbetrag <!-- -->&nbs ...

What are the reasons behind professional web designers opting for absolute paths over relative paths (like for CSS, Javascript, images, etc.)?

It used to be my belief that everyone utilized relative paths, like /styles/style.css. However, I've noticed that certain renowned web designers (such as and ) prefer absolute paths (http://example.com/styles/style.css). This begs the question - why ...

What is the process of converting the string 'dd/mm/yy hh:MM:ss' into a Date format using javascript?

I successfully completed this task. var date = 'dd/mm/yy hh:MM:ss'; var dateArray = date.split(" "); var splitDate = dateArray[0].split("/"); var splitTime = dateArray[1].split(":"); var day = splitDate[0]; var month = sp ...

Error on homepage with Vue Storefront API issue

I have successfully implemented vue-storefront on . However, the standard homepage layout is not displaying correctly due to an error, most likely related to the API. There are no output errors in the terminal, all systems seem to be running smoothly wit ...

Discovering if an item in a list includes any of the elements from another list

I have a collection of phrases ['apple', 'banana', 'orange'] and I am looking to extract the phrases that contain any of the words in ['red', 'yellow', 'green']. I tried using a list comprehensio ...

Required Field Validation - Ensuring a Field is Mandatory Based on Property Length Exceeding 0

When dealing with a form that includes lists of countries and provinces, there are specific rules to follow: The country field/select must be filled out (required). If a user selects a country that has provinces, an API call will fetch the list of provinc ...

Ensure that each function is completed before proceeding to the next one

I've encountered an issue with my react app. After a user submits a form, I need a modal to open and the user's response to be stored in state. Then, based on this response, I have to execute some business logic before finally submitting the form ...

What is preventing the text of the elements from being updated?

My attempt to use JavaScript to change the text of this element has not been successful. The header I am trying to modify is enter image description here var elem = document.getElementsByClassName("headertext"); elem.innerHTML = "hello world"; <ul cl ...

scrolling through a list using .slice choosing an excessive amount of items

Is it possible to create a dynamic pager that can be customized using parameters from the URL? I have noticed that when I hardcode the perTime variable, everything works fine. However, as soon as I try to use a parameter from the URL, the page starts behav ...

Why Does React Material-UI Switch Styling Keep Changing Sporadically?

Currently, I am trying to integrate a switch component from MUI into my code. Strangely enough, upon the initial page load, the switch appears exactly as it should - identical to the one displayed on the MUI site. However, upon reloading the page, it under ...

What is the process for executing "npm run start" in the Azure DevOps release pipeline to initiate a production build for NUXTJS?

I am in the process of setting up CI/CD for my Nuxt application to be deployed on a Windows server. I have completed all the necessary steps such as file copying, npm installation, and running npm build. However, I am facing an issue with the final step wh ...

It seems that Vue3 is limited in its ability to pass multiple props at once

When trying to pass 2 props to my components, I encountered an issue. Both props are strings, and while the posterUrl works fine when passed as the title prop, it doesn't work when passed as the poster prop. This leads me to believe that the data itse ...

Trigger an event within a linked component

I've been working on a connected component where I'm attempting to dispatch the clear action from. Here's a snippet of the code: import {createElement} from 'react'; import reduce from 'lodash/fp/reduce'; import {connect ...

Organizing an array in JavaScript that includes both version numbers and letters, with each letter representing a specific numerical value

If given an array similar to this one: input = [ "1.1", "1.c", "1.b", "1", "D", "b", "4", "2.1.2", "5.1", "3", & ...

How can CORS be activated? Is it through the server or Javascript, and where does this

Currently, I am testing my ReactJS website on localhost:3333 and my ASP.NET Web API 2 on localhost:54690. I am utilizing axios for my AJAX requests, but encountering an error when making a request. XMLHttpRequest cannot load http://localhost:54690/api/ ...

Ways to incorporate a while loop into a randomizing function

I've been diving into JavaScript and managed to create a website that displays random gifs when clicked from an array. Now, my goal is to implement a while loop to prevent duplicate images from being shown consecutively. However, I seem to be facing ...

Learn how to update a Vue.js method dynamically by using the route parameters

After creating a reusable component for image uploads stored in Firebase storage, I encountered an issue when trying to reference the images in the database with section-specific labels. The problem arises when I attempt to name the location for the "down ...