What are the methods for reference paths in Vue single files?
This article mainly introduces you to the relevant information about reference path processing in vue single files. The article introduces it in detail through sample code. It has certain reference learning value for everyone to learn or use vue.js. Friends who need it Let’s learn together with me.
Preface
Vue's single file component is very commonly used when using Vue, and in the development process of vue single file, in the single file The template may involve the processing of file paths, such as , the processing of background in style, etc. The following discusses the src processing of
in several different scenarios, and explains how to correctly reference static resources (such as image processing) during the development process using vue webpack.
As shown below, the following single file component provides examples of referencing image paths in different scenarios (image static resources are stored in src/assets/small.png):
<template> <p id="app"> <!-- 1. 模版中src选项直接写相对路径 --> <img src="./assets/small.png" alt="图片相对路径测试"> <!-- 2. 模版中src选项绑定相对路径字符串 --> <img :src="relative_img" alt="图片相对路径测试"> <!-- 3. 模版中src选项绑定html绝对路径字符串 --> <img :src="absolute_img" alt="图片绝对路径测试"> <!-- 4. 模版中src选项绑定手动加载的图片资源 --> <img :src="smallImg" alt="图片资源测试"> </p> </template> <script> import smallImg from './assets/small.png'; export default { name: 'app', data() { return { smallImg: smallImg, relative_img: './assets/small.png', absolute_img: '/static/img/small.png', }; }, } </script>
The above code snippets give four scenarios of how to use the img tag to reference image resources in the Vue single file component. Of course, not all of these four methods can load image resources correctly.
Case 1:
Bind directly to the src attribute with a relative path in the template. In this case, the image resource can be loaded correctly. We know that in the process of webpack processing vue single file components, vue-loader is mainly responsible for processing *.vue files. The vue-loader resource path processing section in the vue-loader documentation explains how vue-loader handles resource paths in templates. For example: , background: url(), @import, etc. will be processed as module dependencies. That is to say, in these cases, vue-loader automatically handles the resource reference of the path and the final path replacement. The processing of img is as follows:
<img src="./logo.png">
will be compiled by the vue template compiler into:
createElement('img', { attrs: { src: require('./logo.png') }})
This also explains why the image content can be displayed correctly in case 1, because vue-loader Automatically help us with resource introduction and path replacement.
Case 2:
The relative path string variable is bound to the src attribute in the template. In this case, the image cannot be displayed normally. . The reason is that vue-loader cannot identify whether the variable is a path string, so there is no problem of vue-loader automatically introducing resources and path replacement. In this case, the compiled template is still a relative path string. Obviously, without the corresponding resource introduction and wrong path, the image cannot be displayed correctly.
Situation 3:
Many people try to use absolute path variables to import while relative paths cannot be displayed correctly. Obviously, this In this case, the image cannot be displayed because the image resource has not been manually introduced. Note: Many students try to manually introduce resources and then bind src according to the absolute path variable. They find that there are indeed referenced resources under the dist/static/img/ path, but the url-loader in the vue-cli webpack template does not work for img type files. When loading, the processing of hash values is added. In this case, even if we bind an absolute path variable, we still cannot correctly reference the image because it cannot correctly match the image file to which the hash value is added. In this case where images need to be introduced manually, the approach to situation 4 is recommended.
Case 4:
The src attribute in the template is directly bound to the manually introduced image resource. In this case, the image can be displayed correctly. . This method is also used by vue-loader when processing resources corresponding to automatically imported paths.
To sum up, in the vue single file component, the key to correctly displaying an image is:
The image resource is required or imported, that is, it will be used by webpack's url -loader is processed to the final directory
img src attribute needs to be replaced with the final image resource path
The above two points are indispensable. . The reason why the picture can be displayed correctly in case one and case four is that the above two conditions are met in both cases. In case one, vue-loader automatically does this for us, and in case four, we do it manually.
Problems that may be encountered during development:
During development, you may encounter a scenario where a picture list is rendered in a loop. According to the above summary , we can construct an array of image information objects, such as:
<template> <p id="app"> <!-- 1. 模版中 src 选项直接写相对路径 --> <img src="./assets/small.png" alt="图片相对路径测试"> <!-- 2. 模版中 src 选项绑定相对路径字符串 --> <img :src="relative_img" alt="图片相对路径测试"> <!-- 3. 模版中 src 选项绑定绝对路径字符串 --> <img :src="absolute_img" alt="图片绝对路径测试"> <!-- 4. 模版中 src 选项绑定手动加载的图片资源 --> <img :src="smallImg" alt="图片测试"> <!-- 5. 循环加载图片资源示例 --> <img v-for="image in imgList" :key="image.id" :src="image.src" :alt="image.title"> </p> </template> <script> import smallImg from './assets/small.png'; import bigImg from './assets/big.jpg'; export default { name: 'app', data() { return { smallImg: smallImg, relative_img: './assets/small.png', absolute_img: '/static/img/small.png', imgList: [ { id: 1, title: 'test1', src: require('./assets/logo1.png') }, { id: 2, title: 'test2', src: require('./assets/logo2.png') }, { id: 3, title: 'test3', src: require('./assets/logo3.png') }, ], }; }, } </script>
In this way, we can perfectly display the images we render in a loop.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Using the input box to find keywords in jquery
How to implement the specified assignment method using js and jQuery
The above is the detailed content of What are the methods for reference paths in Vue single files?. 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



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() => { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.
