• Vue3中级指南-如何在vite中使用svg图标


    svg图片在项目中使用的非常广泛,今天记录一下我是如何在vue3 + vite 中使用svg图标。

    vite-plugin-svg-icons

    • 预加载 在项目运行时就生成所有图标,只需操作一次 dom
    • 高性能 内置缓存,仅当文件被修改时才会重新生成

    安装

    node version: >=12.0.0 vite version: >=2.0.0

    yarn add vite-plugin-svg-icons -D
    # or
    npm i vite-plugin-svg-icons -D
    # or
    pnpm install vite-plugin-svg-icons -D 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用

    • vite.config.ts 中的配置插件
    import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
    import path from 'path'
    
    export default () => {
      return {
        plugins: [
          createSvgIconsPlugin({
            // 指定需要缓存的图标文件夹
            iconDirs: [path.resolve(process.cwd(), 'src/icons')],
            // 指定symbolId格式
            symbolId: 'icon-[dir]-[name]',
    
            /**
             * 自定义插入位置
             * @default: body-last
             */
            // inject?: 'body-last' | 'body-first'
    
            /**
             * custom dom id
             * @default: __svg__icons__dom__
             */
            // customDomId: '__svg__icons__dom__',
          }),
        ],
      }
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 在 src/main.js 内引入注册脚本
    import 'virtual:svg-icons-register' 
    
    • 1

    如何在组件中使用

    创建SvgIcon组件

    /src/components/SvgIcon/index.vue

    <template>
      <svg aria-hidden="true" class="svg-icon" :width="props.size" :height="props.size">
        <use :xlink:href="symbolId" :fill="props.color" />
      </svg>
    </template>
    
    <script setup> import { computed } from 'vue'
    const props = defineProps({
      prefix: {
        type: String,
        default: 'icon'
      },
      name: {
        type: String,
        required: true
      },
      color: {
        type: String,
        default: '#333'
      },
      size: {
        type: String,
        default: '1em'
      }
    })
    
    const symbolId = computed(() => `#${props.prefix}-${props.name}`) </script> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    icons目录结构
    # src/icons
    
    - icon1.svg
    - icon2.svg
    - icon3.svg
    - dir/icon1.svg 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    全局注册组件
    # src/main.js
    
    import { createApp } from 'vue'
    import App from './App.vue'
    import router from './router'
    
    import ElementPlus from 'element-plus'
    import 'element-plus/dist/index.css'
    
    import svgIcon from "@/components/SvgIcon/index.vue";
    import 'virtual:svg-icons-register'
    
    createApp(App)
        .use(ElementPlus)
        .use(router)
        .component('svg-icon', svgIcon)
        .mount('#app') 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    页面使用
    <template>
        <svg-icon v-if="props.icon" :name="props.icon" />
        <span v-if="props.title" slot='title'>{{ props.title }}</span>
    </template>
    
    <script setup> const props = defineProps({
        icon: {
            type: String,
            default: ''
        },
        title: {
            type: String,
            default: ''
        }
    }) </script> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    获取所有 SymbolId

    import ids from 'virtual:svg-icons-names'
    // => ['icon-icon1','icon-icon2','icon-icon3'] 
    
    • 1
    • 2
  • 相关阅读:
    应用层协议——DNS、DHCP、HTTP、FTP
    LeetCode70. 爬楼梯(C++动态规划简单题)
    CSS逻辑组合伪类
    一文7个步骤从0到1教你搭建Selenium 自动化测试环境
    kali下使用docker建立dvwa靶机
    Connor学Android - RemoteViews
    我开始搞研发管理和项目管理了,发现最难管理的还是人
    Hadoop:yarn资源配置
    Node.js中常用的设计模式有哪些?
    数据可视化,销量第一的新能源汽车是什么?比亚迪新能源汽车销量接近60万辆
  • 原文地址:https://blog.csdn.net/web2022050902/article/details/125446309