Home Web Front-end JS Tutorial Detailed explanation of examples of using alias paths to reference static image addresses in webpack+vue

Detailed explanation of examples of using alias paths to reference static image addresses in webpack+vue

Jan 16, 2018 am 10:29 AM
web Quote

Everyone knows the benefits of aliases in webpack, but in vue templates, problems always arise when using aliases for image addresses. For a long time, no solution was found. I thought it was because of webpack. This article mainly introduces the use of alias paths in webpack+vue to reference static image addresses. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to have a look, I hope it helps everyone.


alias: {
 'src': path.resolve(__dirname, '../src'),
 'assets': path.resolve(__dirname, '../src/assets'),
 'components': path.resolve(__dirname, '../src/components')
}
Copy after login
Copy after login


<template>
 <img src="assets/images/logo.jpg" />
</template>
<script>
import &#39;assets/css/style.css&#39;
</script>
<style>
.logo {
 background: url(asset/images/bg.jpg)
}
</style>
Copy after login

In the above code, you will find that only the introduction of style.css is successful, and the image address and background image address will be resolved. Failure...

After various searches to find the reason (at this time, you will find that Baidu search for these technical contents is really a fighter among garbage), and finally found the reason...

vue-html-loader and css-loader translates non-root URLs to relative paths. In order to treat it like a module path, prefix it with ~

is to alias Add a ~

in front and the final code is written as:


alias: {
 &#39;src&#39;: path.resolve(__dirname, &#39;../src&#39;),
 &#39;assets&#39;: path.resolve(__dirname, &#39;../src/assets&#39;),
 &#39;components&#39;: path.resolve(__dirname, &#39;../src/components&#39;)
}
Copy after login
Copy after login


<template>
 <img src="~assets/images/logo.jpg" />
</template>
<script>
import &#39;assets/css/style.css&#39;
</script>
<style>
.logo {
 background: url(~asset/images/bg.jpg)
}
</style>
Copy after login

means: tell the loader that it is a module , instead of a relative path

Note: Only the static file address in the template and the static file address in the style need to be added ~, in the script, write whatever the alias is defined as.
This is it , I have been struggling with the problem for several months, and finally solved it...

By the way, I will post the list of aliases I use:


alias: {
  &#39;assets&#39;: path.resolve(__dirname, &#39;../src/assets&#39;),
  &#39;src&#39;: path.resolve(__dirname, &#39;../src&#39;),
  &#39;~api&#39;: path.resolve(__dirname, &#39;../src/api&#39;),
  &#39;~components&#39;: path.resolve(__dirname, &#39;../src/components&#39;),
  &#39;~pages&#39;: path.resolve(__dirname, &#39;../src/pages&#39;),
  &#39;~router&#39;: path.resolve(__dirname, &#39;../src/router&#39;),
  &#39;~store&#39;: path.resolve(__dirname, &#39;../src/store&#39;),
  &#39;~utils&#39;: path.resolve(__dirname, &#39;../src/utils&#39;)
}
Copy after login

Related recommendations:

PHP regular code example for obtaining all image addresses on the page

Summary and sharing of regular expression methods for processing image addresses and img tags

How to replace the image address (img src) in a string with JavaScript regular expression

The above is the detailed content of Detailed explanation of examples of using alias paths to reference static image addresses in webpack+vue. 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

Video Face Swap

Video Face Swap

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

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)

How to use block quotes in Apple Notes How to use block quotes in Apple Notes Oct 12, 2023 pm 11:49 PM

In iOS 17 and macOS Sonoma, Apple has added new formatting options for Apple Notes, including block quotes and a new Monostyle style. Here's how to use them. With additional formatting options in Apple Notes, you can now add block quotes to your notes. The block quote format makes it easy to visually offset sections of writing using the quote bar to the left of the text. Just tap/click the "Aa" format button and select the block quote option before typing or when you are on the line you want to convert to a block quote. This option applies to all text types, style options, and lists, including checklists. In the same Format menu you can find the new Single Style option. This is a revision of the previous "equal-width"

C++ compilation error: undefined reference, how to solve it? C++ compilation error: undefined reference, how to solve it? Aug 21, 2023 pm 08:52 PM

C++ is a popular programming language, but during use, the compilation error "undefined reference" often occurs, which brings a lot of trouble to program development. This article will discuss the solution to the "undefined reference" error from both the cause and the solution. 1. Cause of error When the C++ compiler compiles a source file, it will be divided into two stages: the compilation stage and the link stage. The compilation phase converts the source code in the source files into assembly code, while the linking phase combines different source files into an executable file.

How to implement form validation for web applications using Golang How to implement form validation for web applications using Golang Jun 24, 2023 am 09:08 AM

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

Using Jetty7 for Web server processing in Java API development Using Jetty7 for Web server processing in Java API development Jun 18, 2023 am 10:42 AM

Using Jetty7 for Web Server Processing in JavaAPI Development With the development of the Internet, the Web server has become the core part of application development and is also the focus of many enterprises. In order to meet the growing business needs, many developers choose to use Jetty for web server development, and its flexibility and scalability are widely recognized. This article will introduce how to use Jetty7 in JavaAPI development for We

What are web standards? What are web standards? Oct 18, 2023 pm 05:24 PM

Web standards are a set of specifications and guidelines developed by W3C and other related organizations. It includes standardization of HTML, CSS, JavaScript, DOM, Web accessibility and performance optimization. By following these standards, the compatibility of pages can be improved. , accessibility, maintainability and performance. The goal of web standards is to enable web content to be displayed and interacted consistently on different platforms, browsers and devices, providing better user experience and development efficiency.

How to enable administrative access from the cockpit web UI How to enable administrative access from the cockpit web UI Mar 20, 2024 pm 06:56 PM

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

what does web mean what does web mean Jan 09, 2024 pm 04:50 PM

The web is a global wide area network, also known as the World Wide Web, which is an application form of the Internet. The Web is an information system based on hypertext and hypermedia, which allows users to browse and obtain information by jumping between different web pages through hyperlinks. The basis of the Web is the Internet, which uses unified and standardized protocols and languages ​​to enable data exchange and information sharing between different computers.

See all articles