通过vite-plugin-svg-icons 使用SVG图片

一、安装 vite-plugin-svg-icons


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

二、在main.js引入

import 'virtual:svg-icons-register'
   
   
    
    

三、配置SVG图片文件夹

如:

四、在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. // 指定要缓存的文件夹
  9. iconDirs: [path. resolve(process. cwd(), 'src/assets/icons')],
  10. // 指定symbolId格式
  11. symbolId: '[name]'
  12. })
  13. ],
  14. }
  15. })

五、新建svg封装组件,路径参考: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; /* 因icon大小被设置为和字体大小一致,而span等标签的下边缘会和字体的基线对齐,故需设置一个往下的偏移比例,来纠正视觉上的未对齐效果 */
  39. fill: currentColor; /* 定义元素的颜色,currentColor是一个变量,这个变量的值就表示当前元素的color值,如果当前元素未设置color值,则从父元素继承 */
  40. overflow: hidden;
  41. }
  42. </style>

六、按需引入使用


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

七、全局引入使用

在main.js中加入


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

扫描二维码关注公众号,回复: 15080937 查看本文章

一、安装 vite-plugin-svg-icons


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

二、在main.js引入

import 'virtual:svg-icons-register'
   
   
  
  

三、配置SVG图片文件夹

如:

四、在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. // 指定要缓存的文件夹
  9. iconDirs: [path. resolve(process. cwd(), 'src/assets/icons')],
  10. // 指定symbolId格式
  11. symbolId: '[name]'
  12. })
  13. ],
  14. }
  15. })

五、新建svg封装组件,路径参考: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; /* 因icon大小被设置为和字体大小一致,而span等标签的下边缘会和字体的基线对齐,故需设置一个往下的偏移比例,来纠正视觉上的未对齐效果 */
  39. fill: currentColor; /* 定义元素的颜色,currentColor是一个变量,这个变量的值就表示当前元素的color值,如果当前元素未设置color值,则从父元素继承 */
  40. overflow: hidden;
  41. }
  42. </style>

六、按需引入使用


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

七、全局引入使用

在main.js中加入


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

猜你喜欢

转载自blog.csdn.net/weixin_64310738/article/details/129034229