


How to solve the problem of incorrect access path after Vue background image is packaged
This article mainly introduces the solution to the problem of access path error after Vue background image is packaged. The content is quite good. I will share it with you now and give it as a reference.
Case environment
Vue project created through vue-cli scaffolding
I encountered the problem of background image path error when packaging the project. After After some searching on Google, I found that the problem was caused by the image size limit being too small during configuration.
First of all, the error point lies in the url-loader.
// url-loader配置 // build/webpck.base.conf.js { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, loader: 'url-loader', query: { limit: 10000, name: utils.assetsPath('img/[name].[hash:7].[ext]') }
Here is the explanation of the above url-loader configuration. test is a regular matching rule, matching all formats ending with regular rules in the project Files, to put it bluntly, are all pictures (png, jpg, jpeg, gif, svg). Then use url-loader for processing. There is also a rule for processing as follows. When a file no larger than 10000B is base64 transcoded, the image is converted into base64 format. If the image exceeds 10KB, it will be packaged separately in the directory utils.assetsPath('img/[name].[hash:7].[ext]') (you can know this from build/utils.js and config/index.js The path is the static/img directory, and the image name is the value after hashing. There is no static directory under the root directory, so a static directory will be created. As for why we did not see this directory in the end, we will talk about it later). When we create such a directory After that, all the image access paths become the corresponding static/img/'picture name'. At this point, it can be determined that if pictures smaller than 10KB are converted to base64, and pictures larger than 10KB have the picture path changed to static/img/'picture name', then we will continue to clarify the access path.
// 目前我们的目录结构 index.html static |--img |--'picname' |--css |--app.css |--js |--app.js
We know that img is an html tag, and its path is accessed starting from index.html. It can go to static/img/'picture name' The image is accessed correctly, so the path of img is OK. Then app.css accesses static/img/'picture name' incorrectly because there is no static directory in the css directory. This caused the problem of path access failure.
Solution
1. Use small pictures as background pictures (recommendation):
Use pictures smaller than 10KB as background pictures. If there is a picture larger than 10KB as an img picture.
2. Modify the limit value of url-loader (not recommended):
From the above analysis, it can be seen that when the image is converted to base64, there will be no path error problem, ensuring your own background This error can be prevented by converting all images to base64. Change the limit value to the larger value of your largest background image. Convert it to B units
3. Don’t change the css Package it separately (not recommended):
Package it into js directly through css-loader and style-loader, and js automatically creates the style tag. In this way, the access path to the background image is through the index.html path Visited, but this solution is not recommended. It will cause js to be too large, and it is not recommended to convert to base64 for the same reason as pictures that are too large.
4. Use the absolute path of the image address path (recommendation)
Recommendation: Use small pictures as background pictures, and use img tags for large pictures. First of all, we must clarify some differences between background pictures and image img. As far as everyone understands, background pictures are used to modify web pages. For things that have nothing to do with the actual content, use background pictures. If anything related to content should use the img tag, it counts as the content of the web page structure. The modified pictures should be as small as possible, and you can also use image compression and other strategies to reduce the size of the pictures.
Not recommended: The reason why it is not recommended to modify the limit value is that the configuration of url-loader is for the images of the entire project. Modifying the limit value also means that the images with the img tag in the HTML will also be converted to base64. , and the disadvantage of converting to base64 is that it will increase the original size of the image. If you convert a large image to base64, it will cause your js file to be too large, which will increase the time it takes to load js.
About base64
Advantages: base64 is an image represented by a string of string codes. It is loaded together when loading the page or js, reducing image references. A single http request. Students who understand web-side performance optimization know that each http request will take a certain amount of time to establish. For small image requests, the http request establishment time may be longer than the image download itself. Therefore, base64 transcoding of small images is a means to optimize http requests and ensure accelerated page rendering.
Disadvantages: The disadvantage of base64 is as mentioned before. It will increase the size of the image itself. For small images, the increase in size resulting in the increase in js requests can completely make up for the establishment time of one more http request. This The trade-off is cost-effective. But for big pictures, this trade-off is not worthwhile.
Give an example
Example: (The following data are all simulated casually, just take a look at the idea)
If the http creation time is 0.1s each time, the network The transmission is 100KB/s, and the volume increases by 20% each time it is converted to base64;
A 10KB picture is downloaded through an http request in 0.2s. After it is converted to base64, it is 12KB, in the js download, the size is increased by 12KB, so it is increased by 0.12S, so converting to base64 can optimize the page loading speed of 0.08s;
The request speed of a 100KB image through http is 1.1s. After converting to base64, the size is 120KB, which will increase the size of js by 120KB, so the loading time will be increased by 1.2s. Calculated in this way, after converting to base64, the page loading speed cannot be optimized, but the loading speed is slowed down by 0.1s, which is not cost-effective.
Thinking:
During the development process, in addition to dealing with loading speed, we also have to consider the issue of parallel downloading. If they are all in one js, the pictures will not be downloaded until the js is downloaded. That is, after converting to base64, it can be considered that the js and pictures are downloaded serially. With http requests, images can be downloaded in parallel with js. So in fact, smaller pictures are needed to be more cost-effective
The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Introduction to the construction, packaging and publishing process of vue projects
v-for in vue Loading local static image method
vue Implementation of cropping images and uploading them to the server Function introduction
The above is the detailed content of How to solve the problem of incorrect access path after Vue background image is packaged. For more information, please follow other related articles on the PHP Chinese website!

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
