Home Web Front-end JS Tutorial Problems in vue static resource packaging

Problems in vue static resource packaging

Mar 23, 2018 pm 03:19 PM
Pack question

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
Copy after login

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:"../../"     //添加
 })
}
Copy after login

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:

How to use datepicker

Detailed explanation of the use of high-order components of mixin

D3.js creates logistics map

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!

Statement of this Website
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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Share an easy way to package PyCharm projects Share an easy way to package PyCharm projects Dec 30, 2023 am 09:34 AM

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

Clustering effect evaluation problem in clustering algorithm Clustering effect evaluation problem in clustering algorithm Oct 10, 2023 pm 01:12 PM

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.

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

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? Detailed explanation of VSCode functions: How does it help you improve work efficiency? Mar 25, 2024 pm 05:27 PM

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

Teach you how to diagnose common iPhone problems Teach you how to diagnose common iPhone problems Dec 03, 2023 am 08:15 AM

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

Transform Python code into an independent application: the alchemy of PyInstaller Transform Python code into an independent application: the alchemy of PyInstaller Feb 19, 2024 pm 01:27 PM

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

Declaration of Independence for Python Applications: PyInstaller's Road to Freedom Declaration of Independence for Python Applications: PyInstaller's Road to Freedom Feb 20, 2024 am 09:27 AM

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

The ultimate evolution of Python applications: PyInstaller emerges from the cocoon and becomes a butterfly The ultimate evolution of Python applications: PyInstaller emerges from the cocoon and becomes a butterfly Feb 19, 2024 pm 03:27 PM

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

See all articles