Using static assets within VueJS Components

I am currently working on a VueJS project that was customized from a base "webpack-simple" template provided by vue-cli, and it follows the folder structure below:

My main focus right now is developing the "clickableimage.vue" component. However, I'm facing some challenges when referencing assets such as images within this component. Below is the snippet of code for the component:

<template>
<div id="clickableimagecomponent">

<img :src="imageURL" ></img>
<p>Hello There....</p>

</div>
</template>

<script>
 export default {
  name: 'app',
  data () {
   return {
    imageURL:"./dist/loading.gif",
    msg: 'Welcome to Your Vue.js App'
  }
 },

}
</script>

I have configured webpack to build the "clickableimage.js" file and place it in the "dist" folder.

The issue I'm encountering is that when using src (without any vue bindings) for the img tag as "./assets/loading.gif," it works perfectly. Strangely though, even when there is no "loading.gif" file in the dist folder, the code still functions. Upon debugging in Firefox, I noticed the following:

It appears that the file is being loaded from dist/loading.gif, despite not existing in the folder.

Could there be something I'm overlooking? I'm perplexed as to how the browser is able to load the file without it physically being present. Is VueJS performing some sort of magic behind the scenes?

P.S : To rule out any caching issues, I tried opening the URL in an incognito window in Firefox, but it continues to work flawlessly.

Below is my webpack configuration:

Answer №1

My thoughts are swirling around... I believe the issue may be related to imperfections in Hot Module Reloading. If you had it set as "./assets/loading.gif", it would align with test: /\.(png|jpg|gif|svg)$/, resulting in a base64 encoded image being embedded in the script bundle (instead of being placed in the dist folder). When you changed it to "./dist/loading.gif", it might still be looking for loading.gif in the script, which could still be found in the dev environment if you initially ran the dev server with "./assets/loading.gif". While I'm no expert, my theory is that the img-loader in your webpack config is smartly locating the image in one of the webpack chunks that was originally built. If you were to launch the dev server with "./dist/loading.gif", I suspect it wouldn't function as expected.

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

Change the state within the click event handler

One issue I'm facing is dealing with 2 submit buttons in my react form. To differentiate between the two buttons, I need to extract the `id` of the one that was clicked using the `onClick` function. Currently, when trying to set the state with this ` ...

Effective methods for transferring parameters between two separate JavaScript files within an express.js application

Currently, I am working with Express.js and facing a challenge in passing parameters from one JavaScript file to another. How can this be accomplished? The two files involved are 1. process.js var WebPageTest = require('webpagetest'); var wpt ...

Can you make two elements match each other at random?

Currently, I am working on developing a word guessing game where the displayed image should correspond with the word to be guessed. Unfortunately, I am encountering two major challenges in this process. Firstly, I am unable to get the image to display co ...

Guide to triggering an API call upon changing the value in a Vue Multiselect component

Is there a way to trigger an API call when the value changes in a Vue Multiselect component? I want to update the value in the multiselect, make an API call, and display the result in the console. Below is my code snippet. <template> <div> ...

The getStaticProps function will generate an object by fetching data from various URLs

Within my getStaticProps function in next js, I am faced with the challenge of fetching multiple dynamic URLs and exporting the results as props to the component. As these URLs are automatically generated, I do not know how many there will be to fetch. My ...

The following error occurred: Next Auth TypeError: URL is not valid

Every time I run my Nextjs project, I keep encountering an issue related to Next Auth. Despite using version 4.3.1, I am unsure about how to resolve it. Any assistance with next-auth would be greatly appreciated. Although I attempted to update my next-aut ...

How should we structure our JavaScript code: MVC or self-rendering components?

I'm in the process of developing a highly JS-centric web application. The bulk of the work is being carried out on the client side, with occasional syncing to the server using AJAX and XMPP. This is my first venture into creating something of this ma ...

Every key must be a string or number; received a function instead

I encountered a peculiar situation while working with Cucumber: Scenario Outline: Protractor and Cucumber Test InValid Given I have already...... When I fill the <number> .... Examples: | number|... | 3 |... |4 |... Moreover, I ...

Interacting with ref objects and functions in Vue with Leaflet

I have implemented a leaflet map in my vue project. Here is the relevant Vue code: <div style="height:70vh; width:100%; position: relative;"> <l-map ref="map" :zoom="maps_obj.zoom" :center="[maps_obj.latitu ...

abandoning the upload of an item into a THREE.js environment

Currently, I am working on a THREE.js scene where I need to prevent uploading multiple files into the scene simultaneously. The setup involves using Angular to implement the three js scene and canvas as a factory to maintain only one instance of a canvas a ...

Login should only be tried when the error code is 403

I have encountered an issue with checking if the API token is expired. The process involves making a GET call, and if a 403 error is received from the API, then re-login is required. This is what I tried: app.get = async (body) => { return new Pro ...

The error message "require is not defined in React.js" indicates that the required dependency is

As I delve into React coding, the following lines are a part of my code: var React = require('react'); For setting up React, I referred to tutorialspoint. The installation directory is set to /Desktop/reactApp/. My React code is executed from ...

Experiment with jQuery and JavaScript compatibility specifically on Internet Explorer 6

Seeking advice on the most effective method to test my web app using IE6. I currently have IE8 installed on my computer, and am considering utilizing a Virtual Machine. Are there any other alternatives worth exploring? It is imperative that my jQuery and ...

Divergences in Vue.js

Hey there! I am new to Vue.js and have noticed two different ways people are using it. Can someone help me understand the difference? Is example one typically used when creating individual web pages, while example two is preferred for SPA applications usin ...

Enhance your bookmarking experience with Vue mixins that allow you to easily customize the color

Looking for a way to efficiently bookmark pages in my application, I have successfully implemented a feature where I can add pages by their name and URL into bookmarks. Upon clicking the bookmark button, it changes color to indicate that the page has been ...

`AngularJS Integration in Liferay`

Utilizing AngularJS globally within Liferay Portal is a strategy I would employ. The flexibility of AngularJS allows for dynamic views in web applications, enhancing the readability and development speed of the environment. I prefer leveraging the declara ...

Is it possible to use a v-for loop within F7+Vue to dynamically update the title of each accordion item?

Is there a way to dynamically update the title of each accordion-item using a v-for loop in F7+Vue? I want the title of each accordion-item to match the Title property of the objects in my myList array that is being iterated through. Here is an example: ...

Tips on choosing filters and leveraging the chosen value for searching in a Vue application

I am currently working on a Vue JS application that allows users to apply filters and search a database. The user can select or type in filters, such as "to" and "from", which are then used in a fetch URL to retrieve data from a json-server. Once the user ...

Exploring Node.js: Uncovering the Secrets of Checking Dependency Versions

How can I dynamically retrieve the version of an external dependency in JavaScript without manually specifying it? ...

Efficiently Loading AJAX URLs using jQuery in Firefox

setInterval(function(){ if(current_url == ''){ window.location.hash = '#!/home'; current_url = window.location.hash.href; } else if(current_url !== window.location){ change_page(window.location.hash.split('#!/&apo ...