Use SVG images via vite-plugin-svg-icons

1. Install  vite -plugin-svg-icons


   
   
    
    
  1. npm i vite-plugin-svg-icons -D
  2. // or
  3. yarn add vite-plugin-svg-icons -D

2. Introduce in main.js

import 'virtual:svg-icons-register'
   
   
    
    

3. Configure the SVG image folder

like:

4. Configure in vite.config.js


   
   
    
    
  1. //import path,{ resolve } from 'path'
  2. import path from 'path'
  3. import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
  4. export default defineConfig( (command) => {
  5. return {
  6. plugins: [
  7. createSvgIconsPlugin({
  8. // Specify the folder to cache
  9. iconDirs: [path. resolve(process. cwd(), 'src/assets/icons')],
  10. // specify symbolId format
  11. symbolId: '[name]'
  12. })
  13. ],
  14. }
  15. })

5. Create a new svg package component, path reference: src\components\ svg-icon \index.vue


   
   
    
    
  1. <template>
  2. <svg :class="svgClass" aria-hidden="true">
  3. <use class="svg-use" :href="symbolId" />
  4. </svg>
  5. </template>
  6. <script>
  7. import { defineComponent, computed } from 'vue'
  8. export default defineComponent({
  9. name: 'SvgIcon',
  10. props: {
  11. prefix: {
  12. type: String,
  13. default: 'icon'
  14. },
  15. name: {
  16. type : String,
  17. required: true
  18. },
  19. className: {
  20. type : String,
  21. default: ''
  22. }
  23. },
  24. setup( props) {
  25. const symbolId = computed( () => `#${props.name}`)
  26. const svgClass = computed( () => {
  27. if (props. className) {
  28. return `svg-icon ${props.className}`
  29. }
  30. return 'svg-icon'
  31. })
  32. return { symbolId, svgClass }
  33. }
  34. })
  35. </script>
  36. <style scope>
  37. .svg-icon {
  38. vertical-align : - 0.1em ; /* Because the icon size is set to be the same as the font size, and the bottom edge of span and other labels will be aligned with the font baseline, it is necessary to set a downward offset ratio to correct the visual unaligned effect */
  39. fill: currentColor; /* Define the color of the element, currentColor is a variable, the value of this variable represents the color value of the current element, if the current element does not set the color value, it will inherit from the parent element*/
  40. overflow: hidden;
  41. }
  42. </style>

6. Import and use on demand


   
   
    
    
  1. <template>
  2. <SvgIcon name="issue"> </SvgIcon>
  3. </template>
  4. <script setup>
  5. import SvgIcon from "@/components/SvgIcon.vue";
  6. </script>

7. Global introduction and use

Add in main.js


   
   
    
    
  1. import svgIcon from './components/svgIcon/index.vue'
  2. createApp( App). component( 'svg-icon', svgIcon)

1. Install  vite -plugin-svg-icons


   
   
  
  
  1. npm i vite-plugin-svg-icons -D
  2. // or
  3. yarn add vite-plugin-svg-icons -D

2. Introduce in main.js

import 'virtual:svg-icons-register'
   
   
  
  

3. Configure the SVG image folder

like:

4. Configure in vite.config.js


   
   
  
  
  1. //import path,{ resolve } from 'path'
  2. import path from 'path'
  3. import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
  4. export default defineConfig( (command) => {
  5. return {
  6. plugins: [
  7. createSvgIconsPlugin({
  8. // Specify the folder to cache
  9. iconDirs: [path. resolve(process. cwd(), 'src/assets/icons')],
  10. // specify symbolId format
  11. symbolId: '[name]'
  12. })
  13. ],
  14. }
  15. })

5. Create a new svg package component, path reference: src\components\ svg-icon \index.vue


   
   
  
  
  1. <template>
  2. <svg :class="svgClass" aria-hidden="true">
  3. <use class="svg-use" :href="symbolId" />
  4. </svg>
  5. </template>
  6. <script>
  7. import { defineComponent, computed } from 'vue'
  8. export default defineComponent({
  9. name: 'SvgIcon',
  10. props: {
  11. prefix: {
  12. type: String,
  13. default: 'icon'
  14. },
  15. name: {
  16. type : String,
  17. required: true
  18. },
  19. className: {
  20. type : String,
  21. default: ''
  22. }
  23. },
  24. setup( props) {
  25. const symbolId = computed( () => `#${props.name}`)
  26. const svgClass = computed( () => {
  27. if (props. className) {
  28. return `svg-icon ${props.className}`
  29. }
  30. return 'svg-icon'
  31. })
  32. return { symbolId, svgClass }
  33. }
  34. })
  35. </script>
  36. <style scope>
  37. .svg-icon {
  38. vertical-align : - 0.1em ; /* Because the icon size is set to be the same as the font size, and the bottom edge of span and other labels will be aligned with the font baseline, it is necessary to set a downward offset ratio to correct the visual unaligned effect */
  39. fill: currentColor; /* Define the color of the element, currentColor is a variable, the value of this variable represents the color value of the current element, if the current element does not set the color value, it will inherit from the parent element*/
  40. overflow: hidden;
  41. }
  42. </style>

6. Import and use on demand


   
   
  
  
  1. <template>
  2. <SvgIcon name="issue"> </SvgIcon>
  3. </template>
  4. <script setup>
  5. import SvgIcon from "@/components/SvgIcon.vue";
  6. </script>

7. Global introduction and use

Add in main.js


   
   
  
  
  1. import svgIcon from './components/svgIcon/index.vue'
  2. createApp( App). component( 'svg-icon', svgIcon)

Guess you like

Origin blog.csdn.net/weixin_64310738/article/details/129034229
SVG