search
HomeWeb Front-endFront-end Q&AThe image path is wrong after vue-cli3 packaging

With the continuous development of front-end technology, various frameworks and tools are also emerging. In this era of great waves, Vue.js is undoubtedly an existence that cannot be ignored. However, as Vue.js becomes more and more popular, we will encounter some problems. This article will introduce the problem of incorrect image paths when using vue-cli3 to package projects, and provide some solutions.

Problem description

When using vue-cli3 for project development, we can usually use the img tag to introduce images. For example:

<img src="/static/imghwm/default1.png"  data-src="./assets/img/logo.png"  class="lazy" alt="logo" />

It is assumed that the logo.png image is located in the assets directory of the project. The image displays correctly when running the npm run serve command. However, when we run the npm run build command for packaging, we visit the packaged page and find that the image cannot be displayed normally. Through the browser's developer tools, we can find that the image loading fails, and The console gave the following error message:

http://your-domain.com/img/logo.4aebf758.png 404 (Not Found)

Herehttp://your-domain.com refers to the root directory path of the packaged project.

The reason for this problem

We know that Vue.js is based on the component development model. Generally, each component is a separate file, including HTML templates and CSS styles. , JavaScript code, etc. During the development process of Vue.js, we usually use the webpack packaging tool to build and package. Due to the webpack file packaging mechanism, it will package all the resources required by each component into the corresponding JavaScript file, and the browser can only obtain static resources through HTTP requests, so webpack will package the resource files (including images) ) path is converted into an HTTP request path.

In other words, when we use relative paths to reference resource files, the image will be searched in the http://localhost:8080 root directory when the project is running, and when packaging At this time, the path of the image has been packaged as a new path by webpack and placed in the img directory under the root directory. Therefore, when we use webpack packaged files, we need to modify the path.

Solution

Modify the root path of static resources

We can modify the publicPath field in vue.config.js value to replace the root path, thus solving the problem of wrong path.

First, create a new vue.config.js file in the project root directory and enter the following code:

module.exports = {
  publicPath: './'
}

The publicPath here is actually It is to set the packaged static resource storage path. The above code indicates that the static resources and index.html are stored in the same directory.

Use absolute paths to reference images

In addition to solving the problem by modifying publicPath, we can also directly use absolute paths to reference images, thus avoiding the need for relative paths. Come the question. For example:

<img src="/static/imghwm/default1.png"  data-src="/img/logo.4aebf758.png"  class="lazy" alt="logo" />

The / here represents the project root path. Images referenced in this way will not be affected by relative paths.

Place image files in the public directory

We can also place image files in the /public directory. Files in this directory will not be packaged by webpack. Instead, copy it directly to the packaged directory. In this way we can directly reference these images using relative paths.

Summary

Whether it is by modifying the path or using an absolute path to reference the image, the solution is relatively simple. In actual development, in order to reduce the problem of path errors, we can develop good habits during development and try to use absolute paths or place images in public directories. This not only facilitates debugging and maintenance, but also reduces path errors after packaging. Hope this article is helpful to you.

The above is the detailed content of The image path is wrong after vue-cli3 packaging. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is useEffect? How do you use it to perform side effects?What is useEffect? How do you use it to perform side effects?Mar 19, 2025 pm 03:58 PM

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Explain the concept of lazy loading.Explain the concept of lazy loading.Mar 13, 2025 pm 07:47 PM

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?Mar 18, 2025 pm 01:44 PM

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

How does currying work in JavaScript, and what are its benefits?How does currying work in JavaScript, and what are its benefits?Mar 18, 2025 pm 01:45 PM

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

How does the React reconciliation algorithm work?How does the React reconciliation algorithm work?Mar 18, 2025 pm 01:58 PM

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

How do you connect React components to the Redux store using connect()?How do you connect React components to the Redux store using connect()?Mar 21, 2025 pm 06:23 PM

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

What is useContext? How do you use it to share state between components?What is useContext? How do you use it to share state between components?Mar 19, 2025 pm 03:59 PM

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

How do you prevent default behavior in event handlers?How do you prevent default behavior in event handlers?Mar 19, 2025 pm 04:10 PM

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools