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

SVG组件

这个组件是一个封装的 SVG 图标组件,用于统一加载 iconfont(通过 <use> 标签引用),支持传入图标名称、颜色、额外类名,适用于基于 iconfont.cn 等平台导出的 symbol 图标集。

<template><svg :class="svgClass" aria-hidden="true"><use :xlink:href="iconName" :fill="color" /></svg>
</template>
​
<script lang="ts">import { defineComponent, computed } from 'vue'
​export default defineComponent({props: {iconClass: {type: String,required: true,},className: {type: String,default: '',},color: {type: String,default: '',},},setup(props) {return {iconName: computed(() => `#icon-${props.iconClass}`),svgClass: computed(() => {if (props.className) {return `svg-icon ${props.className}`}return 'svg-icon'}),}},})
</script>
​
<style scope lang="less">.nav-icon {position: relative;display: inline-block;margin-right: 12px;font-size: 15px;}
​.svg-icon {position: relative;width: 1em;height: 1em;vertical-align: -2px;fill: currentcolor;}
</style>

二、组件结构分析

template 模板部分
vue复制编辑<svg :class="svgClass" aria-hidden="true"><use :xlink:href="iconName" :fill="color" />
</svg>
说明:
  • 使用 <svg> 结合 <use> 标签,实现 SVG 图标的复用。

  • :xlink:href="iconName":绑定图标路径,如 #icon-home

  • :fill="color":支持自定义图标颜色。

  • :class="svgClass":支持额外类名传入,用于样式定制。

  • aria-hidden="true":屏蔽图标的可访问性(不被辅助工具识别为可交互元素)。

属性(props)说明:
Prop 名称类型是否必填说明
iconClassString图标的名称,如 home
classNameString额外添加的类名(样式扩展)
colorString图标颜色

setup() 返回:
  • iconName:返回 #icon-${iconClass},拼接 symbol id。

  • svgClass:返回一个 class 字符串,基础类名是 svg-icon,可追加自定义类名。

三、使用方式示例

在父组件中使用:
<svg-icon icon-class="home" class-name="custom-style" color="#42b983" />
http://www.dtcms.com/a/319536.html

相关文章:

  • 96-基于Flask的酷狗音乐数据可视化分析系统
  • 微信小程序常见功能实现
  • OpenCV 入门教程:开启计算机视觉之旅
  • uwsgi 启动 django 服务
  • Next.js 15 重磅发布:React 19 集成 + 性能革命,开发者必看新特性指南
  • CentOS 7 安装 Anaconda
  • 秋招笔记-8.7
  • Redis的三种特殊类型
  • 硬盘哨兵pe版本 v25.70.6 中文免费版
  • 【R语言】 高清美观的 MaxEnt 刀切图(Jackknife)绘制——提升论文质量
  • 基于Qt的Live2D模型显示以及控制
  • DAY33打卡
  • 【Unity输入系统】自定义与双击不冲突的单击Interaction
  • 【第八章】函数进阶宝典:参数、返回值与作用域全解析
  • RedisBloom使用
  • 任务进度状态同步 万能版 参考 工厂+策略+观察者设计模式 +锁设计 springboot+redission
  • itextPdf获取pdf文件宽高不准确
  • 设计模式-装饰模式 Java
  • 客户端利用MinIO对服务器数据进行同步
  • VN1 供应链销量预测建模竞赛技巧总结与分享(七)
  • 四边形面积
  • 极简 5 步:Ubuntu+RTX4090 源码编译 vLLM
  • JavaWeb03——基础标签及样式(表单)(黑马视频笔记)
  • 八、基于GD32 Embedded Builder开发GD32VW553(蓝牙广播)
  • 复杂光照场景漏检率↓76%!陌讯多模态融合算法在打电话识别的边缘部署优化
  • 使用Puppeteer轻松自动化浏览器操作
  • PYLON交叉编译:Ubuntu是x86,编译出arm64上运行的程序
  • 无人机航拍数据集|第8期 无人机海上目标检测YOLO数据集3641张yolov11/yolov8/yolov5可训练
  • 下载 | Windows Server 2016最新原版ISO映像!(集成7月更新、标准版、数据中心版、14393.8246)
  • 基于 C 语言的多态机制的驱动架构