When we write vue projects, we will definitely use various pictures, so how can we better use picture resources? Here I will talk about the method I often use. Error demonstrationMaybe you often write this in your code <template> <img :src="src"> </template> <script> export default{ data(){ return { src: require('xxx.jpg') } } } </script> In webpack, require will automatically process resources, which is fine. However, when you put it in vue's data, vue will traverse the data and add responsiveness to src. But most of the time, this src does not need to be responsive, which causes performance waste. By computedTo solve the above error solution, one of the solutions is: computed <template> <img :src="src"> </template> <script> const src = require('xxx.jpg') export default{ computed:{ src(){ return src } } } </script> Compute itself has a cache, which can reduce some performance waste When the image does not change, import it directly<template> <img :src="src"> </template> or <template> <div class="bg"></div> </template> <style> .bg{ background:url("xxx.jpg") } </style> When our image does not change, we can import it directly without assigning a variable. Similarly, you can also dynamically display images by switching class names, which is also better. <template> <div :class="flag ? 'bg1':'bg2'"></div> </template> <script> export default{ data(){ return { flag: true } } } </script> <style> .bg1{ background:url("xxx1.jpg") } .bg2{ background:url("xxx2.jpg") } </style> Switching images via CSS variablesThis is just an idea that I just thought of. Theoretically, CSS variables can store anything, so can they store image addresses? <template> <div class="bg"></div> </template> <script> export default{ mounted(){ // The first method, image address or base64 this.$el.style.setProperty('--bg',`url("http://xxx.com/1.jpg")`) // The second method this.$el.style.setProperty('--bg',`url("${require('../assets/1.png')}")`) } } </script> <style> .bg{ --bg:url('xxx.jpg'); background-image:--bg; } </style> My personal test was successful, this method is also available, and it is better than computed, after all, operating CSS variables costs less. What needs to be noted in this method is that images in CSS are generally written in URLs, so you need to concatenate a string URL (your content). Drawing via CSSThis is actually a digression. Sometimes some images can actually be drawn using CSS. For example, the triangle in the picture below. If you search on Baidu, you will find various CSS triangle generators. This kind of graphic is better drawn using CSS than introducing a picture. To sum up, don't introduce pictures in vue's data, and try to use css SummarizeThis is the end of this article about how to introduce pictures more elegantly in Vue pages. For more relevant content about how to introduce pictures more elegantly in Vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Summary of learning HTML tags and basic elements
>>: How to build a SOLO personal blog from scratch using Docker
Syn attack is the most common and most easily exp...
Table of contents 1. Timestamp to date 2. Convert...
Is there any way to remove spaces from a certain ...
#docker ps check, all ports are mapped CONTAINER ...
Source of the problem: If the slave server is the...
background Not long ago, I made a function about ...
Custom tags can be used freely in XML files and HT...
Original link https://github.com/XboxYan/no… A bu...
Table of contents Preface How to implement Vuex f...
<!--[if IE 6]> Only IE6 can recognize <![...
Table of contents 1. How to switch 2. Dynamically...
Table of contents PXE implements unattended batch...
Recently, I need to package the project for membe...
Vue+iview menu and tab linkage I am currently dev...
1. Use of Iframe tag <br />When it comes to ...