


Detailed explanation of examples of using alias paths to reference static image addresses in webpack+vue
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') }
<template> <img src="assets/images/logo.jpg" /> </template> <script> import 'assets/css/style.css' </script> <style> .logo { background: url(asset/images/bg.jpg) } </style>
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: { 'src': path.resolve(__dirname, '../src'), 'assets': path.resolve(__dirname, '../src/assets'), 'components': path.resolve(__dirname, '../src/components') }
<template> <img src="~assets/images/logo.jpg" /> </template> <script> import 'assets/css/style.css' </script> <style> .logo { background: url(~asset/images/bg.jpg) } </style>
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: { 'assets': path.resolve(__dirname, '../src/assets'), 'src': path.resolve(__dirname, '../src'), '~api': path.resolve(__dirname, '../src/api'), '~components': path.resolve(__dirname, '../src/components'), '~pages': path.resolve(__dirname, '../src/pages'), '~router': path.resolve(__dirname, '../src/router'), '~store': path.resolve(__dirname, '../src/store'), '~utils': path.resolve(__dirname, '../src/utils') }
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!

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

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



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++ 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.

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 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

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.

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

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.

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.
