Cannot display image on nested route in Vue.js

I recently developed a custom component for my application that enables users to select multiple images for products:

<template>
  <!-- Image modal -->
  <div
    class="modal fade"
    id="gallery-multi"
    tabindex="-1"
    role="dialog"
    aria-labelledby="galleryLabel"
    aria-hidden="true"
  >
    <div class="modal-dialog modal-dialog-centered modal-lg " role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="gallery">Gallery</h5>
          <button type="button" class="close" @click="closeModal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <vue-select-image
            :dataImages="dataImages"
            :is-multiple="true"
            @onselectmultipleimage="onSelectMultipleImage"
            :h="'90'"
            :w="'140'"
          ></vue-select-image>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" @click="closeModal()">Close</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import VueSelectImage from "vue-select-image";
import "vue-select-image/dist/vue-select-image.css";
export default {
  name: "selectMultiImg",
  data() {
    return {
      multiID:[],
      dataImages: [
        {
          id: "",
          src: "",
          alt: ""
        }
      ]
    };
  },
  methods: {
    closeModal() {
      $("#gallery-multi").modal("hide");
    },
    onSelectMultipleImage: function(data) {
      
      this.multiID = data.id;
      this.$emit('multiImg', this.multiID);
    }
  },
  components: { VueSelectImage },
  mounted() {
    axios.get("/api/gallery").then(response => {
      this.dataImages = response.data.map((obj, index) => {
        return {
          id: obj.id,
          src: obj.thumb,
          alt: obj.name
        };
      });
    });
  }
};
</script>

I have successfully integrated this component into the main routes of my app like:

{
    path: "/product-category",
    component: require("./components/product/Category.vue")
},

It works seamlessly in the main routes. However, I'm encountering issues when using it in nested routes:

{
    path: "/product/create",
    component: require("./components/product/Create.vue"),
},

The problem arises as images fail to load in the nested route. https://i.sstatic.net/h2F7P.png

https://i.sstatic.net/CE25k.png

In the screenshots above, the original image path is src="img/thumb_dcb7d855594bb92d2fd83108e8a2620e.jpg", however, the highlighted section of the path shows http://localhost:3000/product/img/thumb_dcb7d855594bb92d2fd83108e8a2620e.jpg

This addition to the image path seems to be done by Vue itself. Moreover, there are no errors displayed in the console.

If anyone could assist me in identifying the root cause of this issue and proposing a potential solution, it would be greatly appreciated.

Answer №1

Vue did not include this URL.

Always use absolute paths for images.

For example:

src: '/' + obj.thumb

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

I'm confused as to why this method is returning the state value from the previous state

I am facing an issue with an event handler on a button in React. The component containing the button receives a prop called userInput, which is derived from an input field in the parent component. When using this prop to update the state variable bLength ...

Why does the getComputedStyle function return null for IE11 and Edge when using Kendo React Grid within a Popout window, while it works fine in Chrome, Opera, and Firefox?

Check out my stackblitz demo where I am experimenting with rendering a Kendo React Grid inside a popout window using the react-popout-component. The demo runs smoothly in Chrome, Opera, and Firefox, but encounters issues in Edge and IE11 due to a null valu ...

Angular: Implementing click functionality on an HTML element based on its class

Below my Angular application, I have incorporated a third-party library widget that is being rendered in my component. This is my template: <div> <myWidGet></myWidGet> </div> Within the myWidGet element, there are buttons that ...

Utilizing Node.js and Express to create router instances integrated with a database functionality

My understanding from researching the Express documentation is that when declaring an express.Router(), it creates a single instance of a router where you can assign a routing path and execute logic. The concept of a router being like a mini-app for specif ...

I am looking to extract a specific value from my JSON file

I need to extract specific values from a JSON file. For example, if the JSON file contains id, name, and lastname, I am interested in extracting the id value. fs.readFile("user.json", function (err, data) { if (err) throw err; console.log(data. ...

Creating a vertical line before the boxes in VueJS to label them using HTML and CSS

I am trying to implement a feature where there is a vertical line just before the boxes in order to label those boxes on the y-axis. Here is the code snippet I have written: <div class="rows" v-if="this.system == 'cpu'"> ...

Tips for converting this ajax code to long polling

I am currently using this ajax code and I was wondering if anyone knows how to change it to long polling. Here is the code I am using: var chat = {} chat.fetchMessages = function () { $.ajax({ url: 'ajax/ajax/chat.php', type: 'PO ...

The input of type 'string' cannot be assigned to type 'ChartType'

html code snippet <div style="width: 100%; display: block"> <canvas *ngIf="loaded" baseChart [data]="barChartData" [labels]="barChartLabels" [options]="bar ...

Displaying images in a React app using Node.js

I receive the image from the user as formdata Here is how I structured the Schema for the image The Image has been successfully stored in Monogodb This is my process of fetching image information using Axios I implement code for rendering the image on the ...

Is the UTF-8 string from Google Closure considered valid in this case?

When running the UTF-8 to byte array tests in the Google Closure library, the string provided is: \u0000\u007F\u0080\u07FF\u0800\uFFFF This string is expected to be converted into the following array: [0x00, 0x7F, 0xC2, 0x8 ...

At what point is the JavaScript function expression triggered in this code snippet?

let express = require('express') let app = express(); app.use(express.static('static')); let server = app.listen(3000, function() { let port = server.address().port; console.log("The server has started on port", port); }); I ...

Ways to identify when a user goes offline following an $http request

I'm trying to determine the network connectivity status (online/offline) following an unsuccessful request using the $http service in AngularJs. Despite examining all response parameters in the error handler, I couldn't find any information that ...

What are the steps to incorporate web components into a Vue project with Typescript?

I am currently facing an issue with integrating a web component into my Vue project. Despite seeing the element in the DOM, it appears as an empty tag instead of the button it should be. The first error I encountered was the absence of a declared module. ...

Most effective method to change a specific attribute in every element within a nested array of objects

Below is an example of my data object structure: const courses = [ { degree: 'bsc', text: 'Some text', id: 'D001', }, { degree: 'beng', text: 'Some text&apos ...

The zoom problem in designing with Vue.js WYSIWYG editor

I'm currently facing an issue with my project. It's quite large, so I thought it would be best to share the code as-is to allow you to git clone the project. https://github.com/unique-project-url Let me explain what I'm trying to achieve a ...

There appears to be an issue with the functionality of the external CSS pathway

placeholder image hereplaceholder image hereplaceholder image here I'm encountering a problem and struggling to identify the mistake in my current approach. ...

The setTimeout function in VueJS seems to be malfunctioning and not properly setting the

I am attempting to play sounds in sequence using setTimeout. In my array named challenge, I have (for testing purposes) the values [0,1,2,3]. Additionally, I have a function called play(n, true) for starting the sound and play(n, false) for stopping the so ...

Converting a Google font for compatibility with ThreeJS: A beginner's guide

Is there a way to convert a downloaded Google font from TTF to JSON in order to use it with ThreeJS FontLoader / TextGeometry? import LatoFont from '../assets/fonts/lato-bold.json' const loader = new FontLoader(); const font = loader.parse(LatoF ...

What does the single-threaded nature of JavaScript signify in terms of its intricacies and ramifications?

As someone relatively new to Javascript and JQuery, I recently discovered that Javascript operates on a single-threaded model. This has left me wondering about the implications it carries: What should be taken into account when writing JavaScript code? Ar ...

Instances of duplicated Node cron job executions

Running on an Ubuntu server 18.04 x64 (1 core) with PM2 support, there are two bots functioning. You can view them at this link. One of the bots is set to execute tasks using Cron, particularly focusing on Quotes (MainBotDP). Every day at 9 a.m., a script ...