news 2026/4/16 15:51:04

Vue—— Vue3 SVG 图标系统设计与实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue—— Vue3 SVG 图标系统设计与实现

背景问题:
需要统一管理项目中的图标资源。

方案思考:
使用 SVG 图标系统,便于管理和使用。

具体实现:
首先安装必要的依赖:

npminstallvite-plugin-svg-icons

配置 Vite 插件:

// vite.config.jsimport{defineConfig}from'vite'import{createSvgIconsPlugin}from'vite-plugin-svg-icons'importpathfrom'path'exportdefaultdefineConfig({plugins:[createSvgIconsPlugin({// 指定需要缓存的图标文件夹iconDirs:[path.resolve(process.cwd(),'src/assets/icons')],// 指定symbolId格式symbolId:'icon-[dir]-[name]',})],})

创建 SVG 图标组件:

<!-- components/SvgIcon.vue --> <template> <svg :class="svgClass" aria-hidden="true" v-on="$attrs"> <use :href="iconName" :fill="color" /> </svg> </template> <script setup> import { computed } from 'vue' const props = defineProps({ name: { type: String, required: true }, color: { type: String, default: '#333' }, className: { type: String, default: '' } }) const iconName = computed(() => `#icon-${props.name}`) const svgClass = computed(() => { if (props.className) { return `svg-icon ${props.className}` } return 'svg-icon' }) </script> <style scoped> .svg-icon { width: 1em; height: 1em; position: relative; fill: currentColor; vertical-align: -2px; } </style>

在 main.js 中注册插件:

// main.jsimport{createApp}from'vue'import{register}from'vite-plugin-svg-icons/client'constapp=createApp(App)// 注册 SVG 图标register(app)

使用示例:

<template> <div class="icon-demo"> <SvgIcon name="home" /> <SvgIcon name="user" /> <SvgIcon name="setting" /> <SvgIcon name="search" color="#409EFF" /> </div> </template> <script setup> import SvgIcon from '@/components/SvgIcon.vue' </script>

完整的图标文件结构:

src/ assets/ icons/ home.svg user.svg setting.svg search.svg

其中 SVG 文件示例:

<!-- src/assets/icons/home.svg --><svgxmlns="http://www.w3.org/2000/svg"viewBox="0 0 1024 1024"><pathd="M1024 512c0 282.77-229.23 512-512 512S0 794.77 0 512 229.23 0 512 0s512 229.23 512 512z"fill="#2c2c2c"/></svg>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 14:30:15

Speech Seaco Paraformer版权说明解读:二次开发合规使用须知

Speech Seaco Paraformer版权说明解读&#xff1a;二次开发合规使用须知 1. 引言&#xff1a;关于Speech Seaco Paraformer的定位与价值 你是否正在寻找一个高精度、易用且支持热词优化的中文语音识别工具&#xff1f;Speech Seaco Paraformer ASR 正是为此而生。它基于阿里达…

作者头像 李华
网站建设 2026/4/15 15:54:26

GitHub界面彻底中文化:3分钟让英文编程平台秒变中文环境

GitHub界面彻底中文化&#xff1a;3分钟让英文编程平台秒变中文环境 【免费下载链接】github-chinese GitHub 汉化插件&#xff0c;GitHub 中文化界面。 (GitHub Translation To Chinese) 项目地址: https://gitcode.com/gh_mirrors/gi/github-chinese GitHub作为全球最…

作者头像 李华
网站建设 2026/4/15 18:01:32

科研党福音!CAM++在说话人聚类中的应用示例

科研党福音&#xff01;CAM在说话人聚类中的应用示例 1. 引言&#xff1a;为什么科研需要说话人识别&#xff1f; 你有没有遇到过这样的场景&#xff1f;实验室采集了一段多人对话的录音&#xff0c;比如小组讨论、课堂发言或访谈记录&#xff0c;现在需要把不同人的语音片段…

作者头像 李华
网站建设 2026/4/16 12:39:47

高效焕新Windows 11:tiny11builder轻量级系统打造全指南

高效焕新Windows 11&#xff1a;tiny11builder轻量级系统打造全指南 【免费下载链接】tiny11builder Scripts to build a trimmed-down Windows 11 image. 项目地址: https://gitcode.com/GitHub_Trending/ti/tiny11builder 你是否也曾经历过Windows 11启动缓慢如龟速、…

作者头像 李华
网站建设 2026/4/3 5:42:42

Paraformer-large长时间运行崩溃?内存泄漏检测与修复

Paraformer-large长时间运行崩溃&#xff1f;内存泄漏检测与修复 1. 问题背景&#xff1a;为什么你的Paraformer服务越跑越慢&#xff1f; 你有没有遇到过这种情况&#xff1a;刚启动的Paraformer-large语音识别服务响应飞快&#xff0c;GPU利用率拉满&#xff0c;识别一个30…

作者头像 李华
网站建设 2026/4/16 10:54:02

RePKG:Wallpaper Engine资源处理技术赋能者

RePKG&#xff1a;Wallpaper Engine资源处理技术赋能者 【免费下载链接】repkg Wallpaper engine PKG extractor/TEX to image converter 项目地址: https://gitcode.com/gh_mirrors/re/repkg 工具定位&#xff1a;数字资源的解码桥梁 在Wallpaper Engine生态系统中&am…

作者头像 李华