Problems in vue static resource packaging
This time I will bring you the problems in vuestatic resource packaging. What are the precautions in vue static resource packaging. The following is a practical case, let’s take a look.
①. The default configuration of vue-cli is packaged and deployed to a specific path. The static resource path error is an issue;
②. The css file introduces a large image path error when the static resource is packaged using a relative path.
1. Problem
The default packaging generated by vue-cli scaffoldingConfiguration fileAfter running npm run build, deploy the project to a specific path: such as:
//ip:port/public/springActivity/
Visit at this time:
http://ip:port/public/springActivity/index.html
index.html can be accessed normally, but the referenced js, css and other file server responses are all 404. Check the imported resource path as follows:
http://ip:port/static/css/app. cea07642cd24c0d7a5c4b9b7afc7ff64.css
http://ip:port/static/js/app.815851e87b083afb82bf.js
2. Analysis
It can be seen from the above that the resource packaging path is wrong. The packaged resources use the absolute root directory path, so when the project is deployed to a specific directory, the resource path introduced cannot be correctly parsed.
3. Solve
It is necessary to use relative paths to process static resources when packaging, and change the resource release path configuration in build (config/index.js, build object):
Change assetsPublicPath: '/' to assetsPublicPath: './',
Package it again, and deploy the resources to a specific path, and then access:
At this time index.html can be accessed normally, and at the same time js and css resources can also be accessed normally, but the large image resources in the assets directory introduced in css have an error (server 404)
4. Re-analyze
View the imported image resource path As follows:
http://ip:port/public/springActivity/static/css/static/img/question_bg.61a2825.png
The resource path in the actual project is as follows:
index.html static/ |--js/ |--*.js |--css/ |--*.css |--img/ |--*.png
Obviously the image import path is wrong. Analyzing the image import path, we found that the path has two additional directories "/static/css". It is guessed that the css file in the css directory imports the image path to "/static/img/question_bg.61a2825.png". Check the css file, css The image path introduced in is as follows:
background:url(static/img/question_bg.61a2825.png)
5. When solving the
css file There is a problem with the path in the package. It must be that the resource path of the packaging link is not configured properly. Analyzing the packaging process, the css is introduced in js or written in the vue file. The css file is first processed by less, postcss, etc. After processing, it will Processed by ExtractTextPlugin, ExtractTextPlugin extracts all the css in js into the app.css file.
First set options.extract to false, turn off the css extraction function, package and deploy it to a specific directory again, visit: http://ip:port/public/springActivity/index.html, bang, page Normal display indicates that the Grand Duke is about to be completed.
Analyzing the packaged files, it was found that there was no css file, and all css files were found in the app.js file; css was injected into the index.html file through js, so the image resource path introduced in the css file should be It is relative to the index.html file path, that is: "static/img/question_bg.61a2825.png", which is consistent with the image resource path in the css file below, so the image can be accessed normally.
background:url(static/img/question_bg.61a2825.png)
Now I am sure I know where the problem lies, that is: ExtractTextPlugin did not convert the resource introduction path when extracting the css file, causing the app. css introduces static resources that are "static/img/ .png" relative to the app.css directory. The path relative to index.html is: static/css/static/img/ .png.
Therefore, when using the ExtractTextPlugin plug-in, you also need to configure the static resource path parameters. By querying the information, you know that you can solve this problem by adding publicPath: "../../":
if (options.extract) { return ExtractTextPlugin.extract({ use: loaders, fallback: 'vue-style-loader', publicPath:"../../" //添加 }) }
Packaging After deploying to a specific directory, access the index.html file. Everything on the page is normal. The app.css file is imported normally, and image resources are also imported normally. Check the app.css file to import image resources as follows:
background:url( ../../static/img/question_bg.61a2825.png
After publicPath is configured, the path configuration is added before the image file path introduced in the css file;
The publicPath attribute value is the relative path from the packaged app.css file to the index.html file
Image resources can also be placed directly in the static directory generated by vue-cli to avoid the above problems, but in this way, md5string cannot be added to the image name, which is not conducive to version control
The above is the entire content of this article. I hope it will be helpful to everyone's study. I also hope that everyone will support Script Home.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the use of high-order components of mixin
The above is the detailed content of Problems in vue static resource packaging. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Share the simple and easy-to-understand PyCharm project packaging method. With the popularity of Python, more and more developers use PyCharm as the main tool for Python development. PyCharm is a powerful integrated development environment that provides many convenient functions to help us improve development efficiency. One of the important functions is project packaging. This article will introduce how to package projects in PyCharm in a simple and easy-to-understand way, and provide specific code examples. Why package projects? Developed in Python

The clustering effect evaluation problem in the clustering algorithm requires specific code examples. Clustering is an unsupervised learning method that groups similar samples into one category by clustering data. In clustering algorithms, how to evaluate the effect of clustering is an important issue. This article will introduce several commonly used clustering effect evaluation indicators and give corresponding code examples. 1. Clustering effect evaluation index Silhouette Coefficient Silhouette coefficient evaluates the clustering effect by calculating the closeness of the sample and the degree of separation from other clusters.

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

Detailed explanation of VSCode functions: How does it help you improve work efficiency? With the continuous development of the software development industry, developers' pursuit of work efficiency and code quality have become important goals in their work. In this process, the choice of code editor becomes a key decision. Among many editors, Visual Studio Code (VSCode for short) is loved by the majority of developers for its powerful functions and flexible scalability. This article will introduce some functions of VSCode in detail and discuss

Known for its powerful performance and versatile features, the iPhone is not immune to the occasional hiccup or technical difficulty, a common trait among complex electronic devices. Experiencing iPhone problems can be frustrating, but usually no alarm is needed. In this comprehensive guide, we aim to demystify some of the most commonly encountered challenges associated with iPhone usage. Our step-by-step approach is designed to help you resolve these common issues, providing practical solutions and troubleshooting tips to get your equipment back in peak working order. Whether you're facing a glitch or a more complex problem, this article can help you resolve them effectively. General Troubleshooting Tips Before delving into specific troubleshooting steps, here are some helpful

PyInstaller is an open source library that allows developers to compile Python code into platform-independent self-contained executables (.exe or .app). It does this by packaging Python code, dependencies, and supporting files together to create standalone applications that can run without installing a Python interpreter. The advantage of PyInstaller is that it removes dependency on the Python environment, allowing applications to be easily distributed and deployed to end users. It also provides a builder mode that allows users to customize the application's settings, icons, resource files, and environment variables. Install PyInstal using PyInstaller to package Python code

PyInstaller: Independence of Python applications PyInstaller is an open source python packaging tool that packages Python applications and their dependencies into an independent executable file. This process eliminates dependence on the Python interpreter while allowing applications to run on a variety of platforms, including Windows, MacOS, and Linux. Packaging Process The packaging process of PyInstaller is relatively simple and involves the following steps: pipinstallpyinstallerpyinstaller--onefile--windowedmain.py--onefile option creates a single

PyInstaller is a revolutionary tool that empowers Python applications beyond their original scripting form. By compiling Python code into standalone executable files, PyInstaller unlocks a new realm of code distribution, deployment, and maintenance. From a single script to a powerful application In the past, Python scripts only existed in a specific Python environment. Distributing such a script requires users to install Python and the necessary libraries, which is a time-consuming and cumbersome process. PyInstaller introduces the concept of packaging, combining Python code with all required dependencies into a single executable file. The Art of Code Packaging PyInstaller’s Work
