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

Using Vue CLI to dynamically import modules from project directories

Is it possible to import components from a bundled webpack file using dynamic imports? const url = '${window.location.origin}/assets/default/js/bundle/vue_data.bundle.js'; this.getAxiosInstance().get(url).then(response => { ...

Identify and automatically switch to the mobile site following a selection

Seeking a way to incorporate a redirect on my Joomla site that leads users to a mobile-friendly HTML5 app. After much thought, I've put together the following script: <script type="text/javascript> <!-- if (screen.width <= 800) { ...

Delete multiple selected rows from the table

I need help with removing multiple rows from a table. I've tried the code below but it doesn't seem to work. I'm using DataTables v1.10.9. $('#del_Btn').on('click', function () { // 'table' is the instanc ...

What could possibly be causing my code to execute multiple times?

Within my code, I have the following function that is triggered when the document is ready: $('#file_upload_form').submit(function(){ // show loader [optional line] //if(document.getElementById('upload_frame') == ...

What is the recommended placement for the implicitlyWait method in Protractor?

When implementing implicitlyWait, what is the appropriate location to include browser.manage().timeouts().implicitlyWait(5000); within the test script? ...

Are instance prototypes exhibiting static behavior?

I'm in the process of creating a game for Windows 8 using HTML/Javascript and WinJS, but I'm running into issues with "prototypes" acting statically when they shouldn't. This is my first time working extensively with Javascript and dealing w ...

Change the background color of a particular row in a table using Vue.js to be red

I am having an issue with a regular table - when I click on a specific row, I want only that row to turn red. Below is the code snippet that I have attempted: <tr role="row" v-for="(proxy, key) in this.ProxiesList" @click.prevent=&q ...

Fixing the Angular2 glitch: 'Uncaught TypeError: Cannot read property 'apply' of undefined'

Seeking solutions: How can I fix the "Uncaught TypeError: Cannot read property 'apply' of undefined" error that keeps showing up in the console of my Angular2 application? Any helpful insights are appreciated! ...

The parameter '{ validator: any; }' cannot be assigned to the ValidatorFn type in this context

I am currently experiencing a challenge while attempting to create a custom validator using Angular. I have created a form for my sign-up page and wanted to ensure that the password and confirm password fields match by implementing a custom validator. Des ...

How can I turn off warnings for angular-translation?

A series of translation related warnings are appearing in the browser console, and I am looking to suppress or disable all of these warnings to prevent them from being shown to the user. Warnings: Translation for Department doesn't exist a.(anonymous ...

What is the process for transforming promises into async await syntax?

login() { return new Promise((resolve, reject) => { userCollection.findOne({email: this.data.email}).then((myUser)=>{ if (myUser && myUser.password == this.data.password) { resolve("Congrats! Succe ...

Issue: ParserError encountered due to a Syntax Error found at line 1, column 32

As a beginner in programming, I am encountering an issue. When I run "npm run build" in the Terminal to compress my project in React.js, I encounter this error. Interestingly, I have previously created another project without facing this problem, and I can ...

Angular allows for a maximum time span of 60 days between two date inputs

I am looking to implement a validation in JavaScript or TypeScript for Angular where the start date cannot be more than 60 days after the end date is entered. The requirement is to enforce a "maximum range of 60 days" between the two input dates (dateFro ...

Guide on incorporating a JS file in a React application

I recently obtained a template for my website that includes the following JS file which is being called from my React component. !(function($) { "use strict"; // Hero typed if ($('.typed').length) { var typed_strings = $(&quo ...

Combining two classes into a single class using ‘this’ in JavaScript

I'm encountering an issue where I am unable to figure out how to extend from the third class. So, I really need guidance on how to call the A class with the parameter 'TYPE', extend it with C, and then be able to call getType() with class C. ...

Calling a Coldfusion function from JavaScript/jQuery using a remote server connection

Although I am well-versed in JavaScript/jQuery, I am a newcomer to ColdFusion. After extensive research, I still can't figure out what seems like it should be a simple problem. My goal is to trigger a server-side call to a ColdFusion function from wi ...

Using TypeScript with Vue and Pinia to access the store

Is it necessary to type the Store when importing it to TypeScript in a Pinia Vue project? // Component <p>{{ storeForm.firstName }}</p> // receiving an error "Property 'storeForm' does not exist on type" // Store ...

Determining the moment a user exits a page on Next JS

Is there a way to track when the user exits a Next JS page? I have identified 3 possible ways in which a user might leave a page: Clicking on a link Performing an action that triggers router.back, router.push, etc... Closing the tab (i.e. when beforeunloa ...

What methods can be used to modify the appearance of the cursor depending on its position?

Is there a way to change the cursor to a left arrow when on the left half of the screen and switch it to a right arrow when on the right half, using JavaScript? I am trying to achieve something similar to what is shown on this website. I attempted to acco ...

Combining file and JSON data in React and Express: A guide to uploading both simultaneously

I am looking to send both a file and JSON data from a form using my react handle submission method. const formData = new FormData(); formData.append('File', selectedFile); const data = { name: 'vasu', email: 'example@example ...