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

Vue3+TS中svg图标的使用

安装依赖

 pnpm i vite-plugin-svg-icons -D

配置引入

vite.config.ts

...
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'node:path'const svgIconsPlugin = createSvgIconsPlugin({iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],symbolId: 'icon-[dir]-[name]',  // 支持目录层级:'icon-[dir]-[name]'inject: 'body-last',            // DOM插入位置customDomId: '__svg_icons'      // 自定义容器ID
})
// https://vite.dev/config/
export default defineConfig({plugins: [...,svgIconsPlugin,],...,
})

main.ts

// svg 图标插件模块引入
import 'virtual:svg-icons-register'

应用

简单应用

  • src/assets/ 目录下创建 icons 文件夹
  • 在网上找到想要使用的 svg 图标,复制其 svg 代码
  • 在之前创建的 icons 文件夹下创建 xxx.svg 文件
  • 粘贴复制的 svg 代码到 xxx.svg 文件里
<template><svg style="width: 16px; height: 16px">//#icon-svg文件名  fill字段 指定图标颜色<use xlink:href="#icon-add" fill="red"></use></svg>
</template>

add代表的是创建的svg文件名

参考地址:https://blog.csdn.net/lawidn56/article/details/145929471

封装成组件

  • 准备 svg 文件到目录 src/assets/icons/

  • 创建组件目录文件 src/components/SvgIcon/index.vue

    <template><svg :style="{ width, height }"><use :xlink:href="prefix + name" :fill="color" /></svg>
    </template><script setup lang="ts">
    // 接收父组件传递过来的参数
    defineProps({// 图标前缀(xlink:href属性值前缀)prefix: {type: String,default: '#icon-'},// 图标名称name: String,// 图标颜色color: {type: String,default: '#333'},// 图标大小width: {type: String,default: '16px'},height: {type: String,default: '16px'}
    })
    </script>
    
  • 使用组件
    xxx.vue

    <template><svg-icon width="32" height="32" name="home" color="red"></svg-icon>
    </template><script setup lang="ts">
    import SvgIcon from '@/components/SvgIcon/index.vue'
    </script>
    

注册全局组件

  • 简单注册:main.ts 中注册全局组件,其他 vue 文件直接使用
const app = createApp(App)// 注册 svg 图标全局组件
import SvgIcon from '@/components/SvgIcon/index.vue'
app.component('SvgIcon', SvgIcon)app.mount('#app')
  • 多个组件注册
    • src/components/ 下创建 index.ts 文件
    • main.ts 中注册全局组件
    • vue 文件直接使用

src/components/index.ts

// 引入所有组件
import SvgIcon from './SvgIcon/index.vue'
import Xxx from './Xxx/index.vue'
import type { App, Component } from 'vue'
// 引入所有组件,统一注册
const allGlobalComponent: Record<string, Component> = {SvgIcon,Xxx
}
// 对外暴露插件对象
export default {// 必须叫做 install 方法install(app: App) {// 遍历所有自定义组件Object.keys(allGlobalComponent).forEach(key => {// 注册为全局组件app.component(key, allGlobalComponent[key])})}
}

main.ts

const app = createApp(App)// 引入自定义插件对象,注册整个项目全局组件
import globalComponent from '@/components';
// 安装自定义插件
app.use(globalComponent); // 会触发全局组件的 install 方法app.mount('#app')
http://www.xdnf.cn/news/28531.html

相关文章:

  • 数据分析与挖掘
  • RAGFlow在Docker中运行Ollama直接运行于主机的基础URL的地址
  • opencv 给图片和视频添加水印
  • leetcode57.插入区间
  • Windows系统C盘深度清理指南
  • 车载诊断新架构--- SOVD初入门(上)
  • 23种设计模式-创建型模式之原型模式(Java版本)
  • 医疗器械电磁兼容相关标准
  • 豆瓣图书数据采集与可视化分析(一)- 豆瓣图书数据爬取
  • 性能比拼: Deno vs. Node.js vs. Bun (2025版)
  • C++之虚函数 Virtual Function
  • Redis 的持久化机制(RDB, AOF)对微服务的数据一致性和恢复性有何影响?如何选择?
  • 零基础上手Python数据分析 (18):Matplotlib 基础绘图 - 让数据“开口说话”
  • FPGA——基于DE2_115实现DDS信号发生器
  • FPGA IO引脚 K7-认知4
  • 【java实现+4种变体完整例子】排序算法中【插入排序】的详细解析,包含基础实现、常见变体的完整代码示例,以及各变体的对比表格
  • windows下用xmake交叉编译鸿蒙.so库
  • 交换机与路由器的主要区别:深入分析其工作原理与应用场景
  • hackmyvm-airbind
  • 【人工智能学习-01-01】20250419《数字图像处理》复习材料的word合并PDF,添加页码
  • AI 趋势下 Python 的崛起:深度剖析
  • Nginx 报错403 排查与解决
  • Oracle Recovery Tools修复ORA-600 6101/kdxlin:psno out of range故障
  • CSS中的`transform-style`属性:3D变换的秘密武器
  • 【每日八股】复习计算机网络 Day2:TCP 断开连接时四次挥手及其相关问题
  • 关于大型语言模型的“生物学”
  • 功能性高斯泼溅扩散——DiffGS: Functional Gaussian Splatting Diffusion
  • 203. 移除链表元素
  • B端可视化像企业数据的透视镜,看清关键信息
  • pytorch 51 GroundingDINO模型导出tensorrt并使用c++进行部署,53ms一张图