当前位置: 首页 > news >正文

vue3 项目中优雅的使用 SVG 图标(vite-plugin-svg-icons)

1、背景

vite-plugin-svg-icons 是一个用于 Vite 构建工具的插件,它可以帮助开发者轻松地在 Vue 项目中使用 SVG 图标。该插件的主要功能包括:

  1. SVG 图标自动导入:将指定目录下的 SVG 图标自动导入为 Vue 组件

  2. 性能优化:通过预加载和缓存机制提高图标加载性能

  3. 按需加载:只打包实际使用的图标,减小最终构建体积

  4. 样式自定义:支持通过 CSS 自定义图标颜色和大小

2、Vue 3 使用教程

1、安装插件

npm install vite-plugin-svg-icons -D

2、配置 Vite

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'export default defineConfig({plugins: [vue(),createSvgIconsPlugin({// 指定图标文件夹路径iconDirs: [path.resolve(process.cwd(), 'src/icons')],// 指定 symbolId 格式symbolId: 'icon-[dir]-[name]',})]
})

3、创建 SVG 图标目录

在 src 目录下创建 icons 文件夹,并添加一些 SVG 文件,例如:

  • src/icons/home.svg

  • src/icons/user.svg

  • src/icons/arrow-left.svg

4、注册 SVG 组件

在 main.js 中添加以下代码:

import { createApp } from 'vue'
import App from './App.vue'
import svgIcon from './components/svg-icon';
let instance = createApp(App);
instance.use(svgIcon);
instance.mount('#app')

5、创建 SVG 图标组件

在components/svg-icon下创建SvgIcon.vue

<template>
  <svg aria-hidden="true" :class="className" :width="width" :height="height" >
    <use :xlink:href="symbolId" />
  </svg>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  name: {
    type: String,
    required: true
  },
  className: {
    type: String,
    default: ''
  },
  width: {
    type: Number,
    default: 16
  },
  height: {
    type: Number,
    default: 16
  },
})

const symbolId = computed(() => `#icon-${props.name}`)
</script>

6、全局安装SvgIcon.vue组件

在components/svg-icon下创建index.ts

import { type App } from 'vue';
import SvgIcon from './SvgIcon.vue';
import 'virtual:svg-icons-register';
/*** Svg图标插件*/
export default {install: (app: App): void => {app.component('svg-icon', SvgIcon);},
};

7、使用 SVG 图标

<template><div class="container"><h1>SVG 图标使用示例</h1><div class="icon-grid"><div class="icon-item"><svg-icon name="home" class="icon" /><span>首页</span></div><div class="icon-item"><svg-icon name="user" class="icon" /><span>用户</span></div><div class="icon-item"><svg-icon name="arrow-left" class="icon" /><span>返回</span></div></div></div>
</template><script setup></script><style>
.container {max-width: 800px;margin: 0 auto;padding: 2rem;text-align: center;
}.icon-grid {display: flex;justify-content: center;gap: 2rem;margin-top: 2rem;
}.icon-item {display: flex;flex-direction: column;align-items: center;gap: 0.5rem;
}.icon {width: 2rem;height: 2rem;
  fill: currentColor; // 继承父级颜色
}.icon:hover {color: #42b883; /* 修改icon的颜色Vue 绿色 */
}
</style>

 

 
http://www.hskmm.com/?act=detail&tid=163

相关文章:

  • 自我介绍+软工5问
  • 车道线检测资料
  • 实现Jenkins不同账号只能看到各自任务的权限
  • 6 个最佳无代码 IT 资产管理工具推荐
  • python开发mcp入门
  • 建造者模式进阶:复杂AI服务的优雅构建
  • 代理模式在AI应用中的安全实践:AOP + 限流 + 权限控制
  • ​​高压差分探头:高电压测量的精密之眼​​
  • OCP认证烂大街了吗?别跟风问这个问题了
  • 全国连锁贸易公司数字化管理软件-优德普SAP零售行业解决方案
  • Win7、WinServer2008运行.net8.net4.8程序的解决方案
  • 虚机网络配置基础 - 小
  • 你的开发服务器在说谎-热重载与热重启的关键区别
  • 文件不只是数据-一份稳健的文件处理指南
  • 别再猜了-开始测量吧-一份实用的Web性能指南
  • 软工作业1
  • 避坑指南!Flutter 编译 Android 程序常见问题 + 解决方案,附安全加固技巧
  • [SQL] SQL Server 编写表脚本生成的SQL语句不包含索引以及触发器的解决方法
  • chrome高版本浏览器不兼容driver.execute_script(“return window.performance.getEntries()“)的解决方法
  • 移动端盒子元素实现左右可滑动且竖向页面可滑动
  • 双桶倒水的Python程序
  • 【API接口】最新可用天翼云盘解析接口
  • 搭建GZCTF平台及上传动态flag密码题目过程
  • 微信小程序触发订阅消息
  • TOR内置网桥失效 - Andy
  • PageHelper的使用
  • Linux系统目录结构全解析:基础到大神必读!
  • HarmonyOS实现快递APP自动识别地址
  • Tor浏览器内置网桥失效 - Andy
  • 基于 Rockchip 开发板的 openEuler 镜像的构建