Utilize v-for to dynamically showcase a variety of images on your

My goal is to showcase several JPG pictures located in the src/assets/images folder by utilizing the v-for directive. However, it seems like the browser is having trouble locating them. Interestingly, manually adding the pictures with the same paths works perfectly fine. I have included the code below for reference. See the Screenshot of Page.

<template>
  <div>
    <h2>Success</h2>
    <img src="@/assets/images/1.jpg" width="10%" />
    <img src="@/assets/images/2.jpg" width="10%" />

    <h2>Failure</h2>
    <img
      v-for="(image, i) in images"
      :src="image.url"
      :key="i"
      @click="showProp(image.url, i)"
      width="10%"
    />
  </div>
</template>
<script>
export default {
  methods: {
    showProp: function (url, i) {
      console.log(url, " and ", i);
    },
  },
  data() {
    return {
      images: [
        { url: "@/assets/images/1.jpg" },
        { url: "@/assets/images/2.jpg" },
      ],
    };
  },
};
</script>
<style scoped>
h2 {
  padding: 30px;
}
</style>

Upon clicking on the broken images in the browser, starting from the left image to the right one, the console displays:

@/assets/images/1.jpg  and  0
@/assets/images/2.jpg  and  1

What have I attempted so far?

  • Reviewed previous Stack Overflow posts [1,2] for potential solutions
  • Experimented with different Path-Syntax (@/assets/ OR ../assets/)
  • Removed any aesthetic elements
  • Tried enforcing a type match of image.url using String(image.url)

1: Vue.js image v-for bind
2: How to reference static assets within vue javascript

Additional Notes:

  • This component is embedded within a Vue-Router
  • Inspecting the HTML elements of the rendered page revealed that manual IMG entries were processed differently than dynamic ones. [3]
  • I am using a Win10 machine with local Admin rights
  • I also observed that
    <img :src="path" />
    AND
    data(){return{ path: "@/assets/images/2.jpg"}}
    fails to work.

3: https://i.stack.imgur.com/xhpVc.png

Answer №1

There was a similar issue that I faced some time back, you can solve it by using the following code:

 images: [
    { url: require("@/assets/images/1.jpg")},
    { url: require("@/assets/images/2.jpg") },
  ],

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

Experiencing problems with web page layout when using bootstrap on IE8?

Here is a code snippet that functions correctly in IE9 and later versions, but encounters issues in IE8: <div class="col-lg-5 col-md-5 col-sm-6 col-xs-6" id="div1"> <div class="panel panel-default" style="" id="panel1"> ...

Adding semi-colon in JavaScript with special comments by YUI Compressor

Our team recently implemented YUI Compressor (2.4.8) on our project and it's been performing well. However, we encountered an unexpected issue while minifying JavaScript files that contain special comments. It appears that YUI Compressor is automatic ...

Understanding how to decode querystring parameters within a Django view

In the application I'm working on, there is a search form that utilizes a jQuery autocomplete plugin. This plugin processes the querystring and sends back the suggested item using encodeURI(q). For example, an item like Johnny's sports displays ...

What is the best way to ensure that the state is updated only when the user begins typing in a text

I am currently working on a text editor-related code and my main focus is to update the state of the editor only when the user starts typing in the editor. The state should be updated under the following scenarios: 1. Update the state when the user begin ...

Identifying the Source of a Page Redirection in Javascript: A Step-by-Step Guide

After downloading a free template from the internet, I noticed that there was a link back to the website in the footer. Upon attempting to remove this link, my page started redirecting to the homepage URL of the original website. How can I pinpoint which ...

Tips for incorporating external JavaScript code into React components

I have been tasked with integrating a graphical widget into a React component for a project I am working on. The widget_api code provided by RIPE Stat is required to accomplish this. Previously, in HTML5, the integration was successful using the following ...

Transforming a JSON into a JavaScript object using deserialization

Within a Java server application, there exists a string that can be accessed through AJAX. The structure of this string is exemplified below: var json = [{ "adjacencies": [ { "nodeTo": "graphnode2", "nodeFrom": "graphnode1" ...

Writing a CSV file to AWS S3 proves to be unsuccessful

I have been working with TypeScript code that successfully writes a CSV file to AWS S3 when running locally. However, I have recently encountered an error message: s3 upload error unsupported body payload object NOTES: The code is not passing creden ...

Supporting multiple types for matching object structures is a feature in Jest

I'm currently working on a test using jest to verify if an object key is either a string or a number. It seems like a basic task, but surprisingly there isn't much guidance in the documentation. Test Example: test('Checking asset structure ...

Steps to generate a fresh array from a given array of objects in JavaScript

Looking to transform an existing array into a new array of objects in Vue.js. Here's the initial array : import { ref } from 'vue'; const base_array = ref([ {id: 1, name: Pill, timing: [morning, noon, evening, night]}, {id: 2, name: Ta ...

What is the best way to incorporate a subcategory within a string and separate them by commas using Vue.js?

Is it possible to post subcategories in the following format now? Here is the current result: subcategory[] : Healthcare subcategory[] : education However, I would like to have them as a string separated by commas. This is my HTML code: <div id="sub ...

Is it possible to utilize the AmMap API for developing an Android application?

I am currently in the process of creating a unique application with HTML, JS, and jQuery Mobile that will feature interactive maps. I have been searching the web for APIs to incorporate interactive maps into my project without using Google Maps APIs when I ...

Peer-to-peer Ajax image sharing

Currently, I'm utilizing Ajax to fetch images from a remote server. Initially, I attempted this by directly using the URL of the remote server - resulting in the returned image being a string (given that's how Ajax communicates). To rectify this, ...

Using AngularJS in conjunction with Ruby on Rails is causing compatibility issues

Trying to implement Angular with Ruby on Rails is presenting some challenges. While simple expressions like 1+1 work fine, binding a number or string to a scope seems to be causing issues. I am looking for suggestions on how to resolve this problem. app. ...

Is there a way to update the data on a view in Angular 9 without the need to manually refresh the page?

Currently, I am storing information in the SessionStorage and attempting to display it in my view. However, there seems to be a timing issue where the HTML rendering happens faster than the asynchronous storage saving process. To better illustrate this com ...

Visual Studio Code encounters a JavaScript NPM error that is causing unexpected issues

When using Visual Studio Code, I want my JavaScript program to automatically run through a server as I am learning online. I followed the steps from a tutorial on setting up my IDE and installed Git and Node. Now, when I open Visual Studio and save my Hel ...

Redirecting files from the file system to the emulator is not functioning properly

I am new to Phonegap and need help resolving this issue. In my application, I need to redirect to the List.html file when a button is clicked. Here is the code I have written: button1 function test() { window.location="/home/swift-03/phonegapexa ...

Issue with template updating when utilizing ui-router and ion-tabs in Ionic framework

CODE http://codepen.io/hawkphil/pen/LEBNVB I have set up two pages (link1 and link2) accessible from a side menu. Each page contains 2 tabs: link1: tab 1.1 and tab 1.2 link2: tab 2.1 and tab 2.2 To organize the tabs, I am utilizing ion-tabs for each p ...

Ensuring React's setupProxy functions properly in conjunction with express.js

I've encountered an issue while trying to pass images from express to react. My express.static is correctly set up, so when I visit localhost:5000/public/images/me.jpeg, the image loads in my browser. However, when I attempt to use <img src="/publi ...

``The importance of properly formatting a text string before encoding it into

Currently in the process of converting an XML document to JSON via a PHP backend and then sending it back to the frontend using AJAX. This is achieved through the following code snippet: print_r(json_encode($result_xml)); If you want to view the response ...