search
HomeWeb Front-endJS TutorialThe difference between custom path alias assets and static folders in vue-cli

Write in front:

This is a brief introduction to several small knowledge points of vue-cli. It is suitable for students who are new to vue-cli scaffolding and who do not know much about it. , the big guys took a detour. Friends in need can make a reference. If you like it, you can like it or follow it. I hope it can help everyone.

Static resource processing:

The difference between assets and static folders

I believe many people know that vue-cli has two places to place static resources, which are ## Many people may not be clear about the difference between the #src/assets folder and the static folder.

The files in the assets directory will be processed and parsed into module dependencies by webpack, and only relative path forms are supported. For example, in The difference between custom path alias assets and static folders in vue-cli and
background: url(./logo.png), "./logo. png" is a relative resource path that will be parsed by Webpack as a module dependency. Files in the

static/ directory will not be processed by Webpack: they will be copied directly to the final packaging directory (default is dist/static). These files must be referenced using absolute paths, which are determined through the build.assetsPublicPath and build.assetsSubDirectory connections in the config.js file.

Any files placed in static/ need to be referenced in the form of absolute paths: /static/[filename].

In our actual development, in general:

static holds files that will not change and assets holds files that may change.

How to reference images in js data

Because webpack will reference images as modules, you need to use require to reference images in js, and you cannot directly use strings. form.

js部分:
    data () {
        return {
             imgUrl: '图片地址',//错误写法 
            imgUrl: require('图片地址')//正确的写法
        }
}
template部分:
img标签形式:
<img  src="/static/imghwm/default1.png"  data-src="img"  class="lazy"  : / alt="The difference between custom path alias assets and static folders in vue-cli" >
或者p背景图形式:
<p :style="{backgroundImage: &#39;url(&#39; + img + &#39;)&#39;}"></p>

After talking about pictures, I just want to mention the configuration related to a picture of vue-cli. The configuration in the picture below means: perform base64 conversion below the 10000b picture, so if there are some relatively small ones in the project The icon no longer needs to be processed by the image wizard

webpack+vue custom path alias

vue-cli uses webpack, you can also use the webpack custom alias function to customize the alias This function

When you are nested in multi-layer folders, you don’t have to find the path layer by layer. You can directly use the custom alias to find the location of the file.

Setting method:

**Setting address: **webpack.base.conf.js file under the build folder


Specific settings:

resolve: {
    extensions: [&#39;.js&#39;, &#39;.vue&#39;, &#39;.json&#39;],
     alias: {
        &#39;vue$&#39;: &#39;vue/dist/vue.esm.js&#39;,
        &#39;@&#39;: resolve(&#39;src&#39;),
        &#39;static&#39;:path.resolve(__dirname, &#39;../static&#39;),//增加这一行代码
        }
    },

How to use:

When using it, add a '~' in front of it like B in the screenshot below. Although webstorm here prompts an error, we You don't need to worry about it, the code will run normally.

Interpretation:

Here, 'static' is given an address, so when the path is introduced in the program, '~static' can directly replace the path'.. /static', personal test, even if there are multiple levels of nesting, the path can be found successfully.

The difference between custom path alias assets and static folders in vue-cli

Clean up useless plug-ins in the project

Many people, like me, will install a lot of plug-ins at the beginning, and then end up not using them in the project. . So many plug-ins have been installed before, and you have even forgotten which plug-ins you have installed?

package.json

At the location shown in the picture above, all module dependencies installed by our project are in this pageage.json file. When we When you need to sort out your own dependencies, you can check this file to see if there are any dependencies that are no longer useful. You can use the command line

npm remove module name to delete useless modules.

The difference between –save-dev and –save

Some of the above dependencies are modules that are only used in the development environment, and some are modules that will continue to be relied upon after the project goes online. The difference between them is that when we usually install module dependencies:

--save-dev and --save

When you use

-- When save-dev installs dependencies, it will be placed under the devDependencies object in package.json. On the contrary, when you use --save to install dependencies, it will appear under the dependencies object.

Summary:

* –save-dev is something you rely on when developing, –save is something you still rely on after publishing. *

I have written two articles before about vue-cli configuration. Students who need it can take a look:

Teach you how to use vue-cli scaffolding

Reference jQuery, bootstrap and use sass and less to write css in vue-cli scaffolding

Afterword

The above is the content of this article, which is some of my practice projects for a period of time Small accumulation, there will be some content in the follow-up, because the project is tight, I may meet you later.



The above is the detailed content of The difference between custom path alias assets and static folders in vue-cli. 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
Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

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.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

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

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

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.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

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.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

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.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

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 Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.