Utilize Vue-cli 3.x to load static resources

In my vue-cli 3 project, I have organized the static assets in the public directory. When compiled and built on localhost, all assets load successfully, except for some images not appearing in the browser.

Despite guyana-live-logo.png, slide-1.jpg, and 970x250-ad.png being loaded correctly, only the logo and ad image show up in the browser as depicted below:

What I've attempted

  1. Alteration of the image reference method.

Original - works for most images

<img src="/img/slide-1.jpg"/>

Test 0 - appended a hash to the image name (slide-1.1b0ahsa.jpg) but still no display in the browser

<img src="../../../public/img/slides/slide-1.jpg">

Test 1 - used v-bind

<img :src="'/img/slide-1.jpg'"/>
  1. Relocation of the image within the src directory and component sub-directory, both without success.

  2. Updating vue-loader

  3. Production build with sole serving of the /dist folder

Important points

  • No errors shown on console or bug tracking tool.
  • The image format doesn't seem to be the issue, as some .png/.jpg files load while others don't.
  • Some JavaScript files are also affected and called like this:
    <script type="text/javascript" src="<%= BASE_URL %>js/script.js"></script>
    in public/index.html

Answer №1

To ensure the images are accessible, they should be located in the same directory as the file you are trying to access them from, or in a subdirectory like the Components directory.

Have you attempted accessing the image through its URL? Use

<img src="http://localhost:8080/img/guyana-live-logo.png" />
and see if it works for you.

This method should be effective, but there might be reasons why you prefer not to use it this way.

An alternative approach you can consider is:

<script>
import image from './img/slide-1.jpg'
...

In your Vue data:

  data() {
    return {
      img: image,
    };
  },

In your HTML:

<img :src="image"/>

By following these steps, you can resolve any challenges faced when attempting to access images while working with Parcel Bundler.

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 can I create an onclick link within an alert message using JavaScript, even when the alert is not consistently displayed?

I am facing an issue with an alert that is supposed to appear under specific conditions on my website. The alert includes a link that should trigger some code when clicked. However, I keep getting an error message saying "ReferenceError: doTheThing is not ...

Error: The provided `anchorEl` property for this component is invalid

While working on my React 18.2 app with MUI 5.10.5, I encountered an issue trying to create a <Menu /> element that should open when a button is clicked. The menu does appear, but the positioning seems off as it displays in the top-left corner of the ...

OpenLayers had trouble handling the mouse event in Ionic

I am attempting to handle a double mouse click event on OpenStreetMaps by utilizing the code below: const map = new OpenLayers.Map("basicMap"); const mapnik = new OpenLayers.Layer.OSM(); const fromProjection = new OpenLayers.Projection("EPSG:4326"); // ...

Storing uploaded files with multer in a Node.js environment using JavaScript

I am facing an issue with uploading multi part form data containing one file input and several text inputs. I have a directory named 'public' which contains a sub-directory named 'uploads'. My goal is to store all multer uploads in the ...

Setting MUI input value within a loop using an array value in React JS: A step-by-step guide

Recently, I created a React js demo using React MUI. To handle inputs, I created a separate component called InputText.js for each input field. The issue I encountered is when I tried to use this component to display multiple education fields stored in an ...

Node.js unleashes the power of Ajax

I have seen some people ask this question before, but I am having trouble understanding the responses :/ I am working with node.js and really want to utilize Ajax in my project. Here is a snippet of my code: var $ = require('jquery'); var http ...

Using Elasticsearch's bulk feature to set unique identifiers(_id) for documents

Whenever I attempt to insert documents into elasticsearch with a set _id, I encounter the following error: The field [_id] is considered a metadata field and cannot be included within a document. It should be utilized in the index API request parameters in ...

Connecting Vue to external data sources

My goal is to utilize Vue as a thin layer to connect existing model objects to a view effortlessly. Below, there's a simple app that highlights my issue. I have a GainNode object from the Web Audio API, and I aim to link its value to a slider. In An ...

Transitioning from GeometryUtils.merge() to geometry.merge()

When upgrading from r66 to r67, a message pops up stating: DEPRECATED: GeometryUtils's .merge() has been moved to Geometry. Use geometry.merge( geometry2, matrix, materialIndexOffset ) instead. The transition doesn't seem straightforward beca ...

What is the best way to implement OTP expiration time in Next.js using Firebase?

Could anyone please help me with setting the OTP expire time in Next.js using Firebase? I have searched through the Firebase documentation but haven't been able to find a solution to my issue. Below is the code I am using to send the OTP: const appV ...

Verify the presence of a JSON object in Postman

I'm looking to create a test in Postman to validate the presence of JSON keys in the server response I've received. Here is the response: { "Result": 0, "ResponseStatus": { "ErrorCode": null, "Message": null, "StackTrace": null ...

Transferring data from a JavaScript struct array to GLSL

Struggling to configure the values of a structure containing all the lights in my WebGL app using JavaScript. The layout of the structure is as follows: struct Light { vec4 position; vec4 ambient; vec4 diffuse; vec4 specular; vec3 spo ...

Display loading icon in AngularJS during template retrieval

Imagine I have this specific directive: angular .module('app.widgets') .directive('myCalendarRange', myCalendarRange); function myCalendarRange () { var directive = { link: link, templateUrl: '/template/is/located ...

`initMap` does not exist as a function

Hey everyone, I need some help: I recently integrated the Google Maps API into my AngularJs project and encountered an error in the console: Uncaught message: "initMap is not a function" name: "InvalidValueError" This occurs when the Google Map API is ...

Issue with vue-select and buefy CSS not rendering properly in production environment with NUXT.JS

While using the buefy autocomplete component with nuxt.js, I encountered an issue where the CSS file imported worked fine on localhost but failed to render properly on the live server. Surprisingly, the same problem occurred with the vue-select autocomplet ...

What could be causing my Vue Router to not direct to my component?

Hey there, I'm fairly new to Vue and trying my best to navigate without diving into NPM. Please bear with me as I may ask some basic questions. Let's skip the template for now since I have a component set up and tested outside of routing: var T ...

JavaScript HTTP Requests

I came across this AJAX example in Stoyan Stefanov's book Object Oriented JavaScript on page 275. The example involves requesting three different files. I have a few questions that I was hoping someone could help me with! What does the line xhr.se ...

Trouble Displaying DHtmlX Scheduler Events on Safari and Internet Explorer

I have recently embarked on the journey of building a nodejs application and decided to incorporate DHtmlX Scheduler as my calendar. To my dismay, I encountered an issue where the events are not displaying on Safari or IE, despite working perfectly fine on ...

What is the best way to initiate an animation once the one before it has finished?

I am facing an issue with my animation function where all animations play simultaneously. This is not the desired behavior; I would like to play each animation after the previous one ends. Can someone guide me on how to achieve this? canvas = document.get ...

Issue with JavaScript variables (I presume) - there seems to be a conflict when one variable is used alongside another

I am attempting to conduct a test that requires displaying the number of attempts (each time the button is pressed) alongside the percentage of successful attempts, updating each time the button is clicked. However, I've encountered an issue where onl ...