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:

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

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:

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

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

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

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:

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

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

Updating state from a React mapping component: A step-by-step guide

I am working on an API that needs data filtering and then I want to place the filtered data into a state export default class ModifyPage_Single extends React.Component { constructor(props) { super(props) this.state = {data:[],idd:" ...

Designing a carousel-style menu list with navigation buttons for moving forward and backward

I'm running into some trouble while attempting to create a carousel. Firstly, the issue I am facing is that when you continuously click on the Next button, the function keeps working even after reaching the last item. I'm not sure how to make th ...

Tips for selecting a checkbox with Puppeteer

I've implemented the code in this way: await page.$eval('input[name=name_check]', check => { check.checked = true; }); This code is intended for multiple checkboxes. However, I need it to work for a single checkbox only. Is there a way ...

Unwrapping React: Mastering the Art of Prop Extraction and Sharing

Imagine I have 2 input elements below, both with the same props except for the className <ReactCardFlip> <input type="text" maxLength={1} className={inputStyles.input} defaultValue={value} ...

AngularJS scope watch isn't firing as expected

In the scope, I store a filtering string for dates. $scope.myModel = {date:""}; Utilizing a jQuery datepicker, <input date-value="myModel.date" date-picker /> This directive updates the string through AngularJS - Attribute directive input value c ...

Use Javascript to display an image based on the date, otherwise hide the div

I'm looking to implement an image change on specific dates (not days of the week, but actual calendar dates like August 18th, August 25th, September 3rd, etc). Here's the div I'm working with: <div id="matchday"> <img id="home ...

Submitting an mvc partial view form to send data from the parent view

I am currently working on a MVC 5 App where I have a Parent View that includes a Partial View, allowing users to load images. Upon submitting, the Parent view calls a .Ajax function defined within it, which in turn calls a Method/Controller. My requireme ...

(Express JS) What is the correct way to integrate another module into my router? (I am consistently encountering an undefined reference error)

I am currently working on a basic PDF reader application that utilizes the pdf.js library from Mozilla. The user is expected to select a file, after which the website should automatically redirect to the /reader page displaying the PDF. However, I am facin ...

Trigger a Vue.js action once an element is generated using v-if

One of the challenges I am facing is loading an element based on a condition (v-if). <div id="food-content" v-if="activeFood" v-cloak> To load it, I use this line of code: app7.activeFood = food; After the element is instantiated, I aim to apply ...

Polyfill for window.showOpenFilePicker function

Could anyone recommend a polyfill for the window.showOpenFilePicker method? For reference, you can check out the documentation on MDN. ...

How can we activate navigation on a mobile browser?

I am currently working on a small HTML5/JavaScript website designed to be accessed by mobile browsers on devices such as Android and iPhone. I am utilizing the geolocation feature of HTML5 to obtain the user's current location, but my goal is to then ...

Troubleshooting Vue.js unit test issues caused by log messages from internal components

I am working with a component structured like this: <template> <p>{{ message }}</p> <internal-comp></internal-comp> </template> <script> import InternalComp from './components/InternalComp.vue'; ...

Update the text based on the selected input

Currently, I am working on creating an RFQ interface for an OTC desk. My goal is to retrieve the price for a specific cryptocurrency that is chosen using the <v-select> element. To achieve this, I am attempting to send a GET request to the price endp ...

My code gets disrupted when I switch between ids and classes

I'm currently learning about javascript and jquery, attempting to implement various scripts. I successfully executed the following script: jQuery(document).ready(function($) { var scroll_start = 0; var startchange = $('#homepage-header' ...

What strategies can be used to steer clear of overhyped products after adjusting state?

I have a function that retrieves data from Firebase. After getting the data, I want to set it into a state. So, I create an array and push all the data into it. Then, I update my state with this array. However, when I log or render this state, I encounter ...

The shown.bs.tab event is causing an issue where the previous active tab cannot be retrieved

Reference from https://getbootstrap.com/docs/4.0/components/navs/#events states that I am utilizing e.relatedTarget in the shown.bs.tab event to retrieve the previous active tab for applying some CSS styles. However, currently, e.relatedTarget is returning ...

Enforce the selective inclusion of specific classes in TailwindCSS cherry picking

Currently in my Vue project with TailwindCSS and Vite, I am trying to implement a dynamic width class based on the size of an array. const array = [1, 2, 3]; const width = computed(() => `w-1/${array.length}`); <div :class="width">Conte ...

What is the best way to implement a modal that can toggle dark mode using the Konami code, with the added functionality of a close button?

Recently, I attempted to create a Modal window that would activate when the Konami code (↑↑↓↓←→←→BA) is typed. As someone new to JavaScript, I'm still learning and open to feedback. While I have the coding part figured out, I need assi ...

Influencing location preferences in Google's place search

I'm currently implementing a Google Places search feature and I need to enable "Location Biasing" for the GCC countries (UAE, Saudi Arabia, Oman, Kuwait & Bahrain). My goal is to achieve the functionality described in the following link: https://deve ...

Node.js encountered an error: Address already in use. The specified port 8080 is currently being utilized by another application

My application was not functioning properly, and upon running the command "npm start", an error was thrown: Error: listen EADDRINUSE: address already in use :8080 Even after restarting my EC2 instance and attempting the command again, I encount ...