A guide on embedding static content in the index.html page

During my project, I utilized vue-cli with webpack and encountered an issue when trying to load an image in index.html. Here is how it was done initially:

<html>
    ...
    <style>
        .tag1 { background-image: url('<%= require('@/assets/icons/a123.svg') %>'); }
    </style>
    ...
</html>

Now, I am transitioning to Vite and need to figure out how to load the icon in index.html using this new build tool. I attempted url(/assets/icons/a123.svg), but unfortunately, it resulted in a 404 error.

Answer №1

Transfer the SVG file to the /public directory, then you will be able to reach it using

<img src="/a123.svg" />

Answer №2

To easily manage assets in your project, consider utilizing a function, refer to the Vite documentation

const useImage = ((url) => {
  return new URL(`/src/${url}`, import.meta.url).href;
});

Next, establish a global property in your main.js file:

app.config.globalProperties.$image = useImage;

You can then easily utilize this by calling:

$image(imageUrl)

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

The ng-repeat directive fails to acknowledge changes in the model

I'm having trouble iterating over the properties of a model that I am updating. Below is the code snippet from my controller: app.controller('VariantsController', ['$http', function($http){ var ctrl = this; this.cars = []; ...

Modifying the geometry of a three.js object

I have been working on changing the geometry of objects in a three.js scene. I have written code that almost works, where a mouse click triggers the change. However, I am facing an issue where the shape is only updated on the first mouse click, even though ...

Is there a way to host a Vue app bundled with webpack in Django without having to recompile for every update?

After setting up my Django project, I decided to develop a Vue frontend to complement it. I initiated the Vue app using vue init webpack myproject-frontend. Working on both projects separately was simple - running python manage.py runserver for Django and ...

Finding the actual path in any scenario using JavaScript ($.ajax)

Here is the structure of my project: Sil -> css -> js -> clients -> index.php -> response.php index.php I am facing an issue related to the folder 'clients'. My website URL can have different va ...

What is the best way to trigger the Javascript blur event following a click event that results in the element losing focus

Encountering this issue multiple times has left me dissatisfied with the solutions I've implemented in the past. The challenge lies in dealing with an input box that triggers a validation process on blur, while also having a button that populates the ...

What steps should be taken to ensure input validation in jquery SmartWizard when loading content via ajax requests?

I have successfully implemented jQuery SmartWizard to validate input type text. However, when the input text is loaded from Ajax, the validation does not work. Code for validation: // Toolbar extra buttons var btnFinish = $('<button></butto ...

Transferring data and parameters between the Client-Side and Server-Side

I am currently working on setting up an express.js server, but I am struggling to figure out how to send and receive data effectively. Specifically, I have an input field where users can enter their email addresses. I want this email address to be sent to ...

How can I expand my Jquery slideshow to occupy the entire screen?

I am working on stretching my JQuery slideshow to fit the entire browser, but I am running into some difficulty. I have downloaded the jQuery Vegas plugin, but I can't seem to figure out what the problem is. I would like my slideshow to resemble the o ...

Use regular expressions and JavaScript to enclose a series of English letters within a wrapper

I am looking to enclose a series or cluster of consecutive English characters within a <span> tag. In simpler terms, I aim to alter the appearance of English language in my writing. Therefore, I need to identify English characters and surround them ...

What is the functionality of ngCloak?

<html ng-app> <body> <p ng-cloak>{{foo}}</p> </body> </html> My interpretation: The HTML page is displayed. Once the DOMContentLoaded event is triggered, Angular begins bootstrapping. During the compilation ph ...

What method can I use to locate the double quote option within an HTML select element using jQuery?

Here is an example of the select: <select id="id0" name="name0"> <option value='30" size'> 30" size </option> </select> The select contains double quotes. I am trying ...

The sendKeys() method in Protractor is failing due to the element being hidden/not

Hi there! I am currently new to automated testing with protractorJS for an angularJS homepage. While the code I've written so far has been successful, I'm facing an issue where I'm unable to input keys into the search field. After running th ...

Repeatedly animate elements using jQuery loops

Whenever I click a button, a fish should appear and randomly move over a container at random positions. However, in my current code, the animation only occurs once and does not repeat continuously. I want to create a generic function that can be used for m ...

Issue: app.database function is not recognized with Firebase

I'm attempting to integrate Firebase realtime database into my Vue application, but I keep encountering the following error: TypeError: app.database is not a function This is what my code looks like: File: Firebase.js var firebase = require(' ...

Combining two observables into one and returning it may cause Angular guards to malfunction

There are two important services in my Angular 11 project. One is the admin service, which checks if a user is an admin, and the other is a service responsible for fetching CVs to determine if a user has already created one. The main goal is to restrict ac ...

JavaScript code for the submit button to proceed or halt the form submission

Within my JSP file, I have the following code snippet: <input type="submit" value="Transfer ULD" onclick="doSomething();" name="_eventId_transferULDTransition"/> The doSomething() function mentioned above is a JavaScript method. function doSomethi ...

Utilize Create React App and Leaflet Maps to showcase individual user profiles alongside detailed location maps

I have developed a simple app that showcases user cards with information obtained from a JSON API along with a map featuring markers for each user's location. I have successfully implemented this functionality. However, I am facing challenges in link ...

Unable to install local dependency using npm

After successfully using npm install react-financial-charts, I decided to include the package locally for specific reasons. I checked out the master branch of react-financial-charts from Github, resulting in two folders: C:\Users\user\projec ...

The processing-js library threw a Reference Error indicating that the Navigator could not be found

I am looking to utilize processingJS as an npm package in a nodeJS server for deployment on MS Azure. I am using VS15 and encountering difficulties referencing it: var pjs = require('processing-js'); var http = require('http'), fs = r ...

Guide on verifying the status of a switch toggle using Selenium and C#

I am having trouble verifying the current state of a switch ('on' or 'off') using selenium. The toggle appears in the 'on' position when the webpage loads, but it affects filter results in a table when switched off. I am unabl ...