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
CSS: Is it bad to use ID selector?CSS: Is it bad to use ID selector?May 13, 2025 am 12:14 AM

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5: Goals in 2024HTML5: Goals in 2024May 13, 2025 am 12:13 AM

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

What are the main areas where HTML5 tried to improve?What are the main areas where HTML5 tried to improve?May 13, 2025 am 12:12 AM

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

CSS ID and Class: common mistakesCSS ID and Class: common mistakesMay 13, 2025 am 12:11 AM

IDsshouldbeusedforJavaScripthooks,whileclassesarebetterforstyling.1)Useclassesforstylingtoallowforeasierreuseandavoidspecificityissues.2)UseIDsforJavaScripthookstouniquelyidentifyelements.3)Avoiddeepnestingtokeepselectorssimpleandimproveperformance.4

What is thedifference between class and id selector?What is thedifference between class and id selector?May 12, 2025 am 12:13 AM

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

CSS IDs vs Classes: The real differencesCSS IDs vs Classes: The real differencesMay 12, 2025 am 12:10 AM

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

CSS: What if I use just classes?CSS: What if I use just classes?May 12, 2025 am 12:09 AM

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and Class Selectors in CSS: A Beginner's GuideID and Class Selectors in CSS: A Beginner's GuideMay 12, 2025 am 12:06 AM

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool