在vue3 + ElementUI 项目中自定义 Icon

前言:

在 iconfont阿里巴巴矢量图标库 中复制 svg 代码,封装成一个组件。

正文:

javascript 复制代码
<template>
    <svg :style="{ width: sizeCss, height: sizeCss, color: color, display: 'inline-block', verticalAlign: 'middle',marginRight: '4px' }"
        viewBox="0 0 1024 1024" aria-hidden="true" focusable="false">
        <path
            d="M637.155556 455.111111c-20.48 0-34.133333 13.653333-34.133334 34.133333 0 75.093333-61.44 136.533333-136.533333 136.533334H261.688889c-75.093333 0-136.533333-61.44-136.533333-136.533334 0-75.093333 61.44-136.533333 136.533333-136.533333h102.4c20.48 0 34.133333-13.653333 34.133333-34.133333S384.568889 284.444444 364.088889 284.444444H261.688889C145.635556 284.444444 56.888889 373.191111 56.888889 489.244444s88.746667 204.8 204.8 204.8h204.8c116.053333 0 204.8-88.746667 204.8-204.8 0-20.48-13.653333-34.133333-34.133333-34.133333z"
            fill="currentColor"
            p-id="31867"></path>
        <path
            d="M807.822222 284.444444h-204.8C486.968889 284.444444 398.222222 373.191111 398.222222 489.244444c0 20.48 13.653333 34.133333 34.133334 34.133334s34.133333-13.653333 34.133333-34.133334c0-75.093333 61.44-136.533333 136.533333-136.533333h204.8c75.093333 0 136.533333 61.44 136.533334 136.533333 0 75.093333-61.44 136.533333-136.533334 136.533334h-102.4c-20.48 0-34.133333 13.653333-34.133333 34.133333s13.653333 34.133333 34.133333 34.133333h102.4c116.053333 0 204.8-88.746667 204.8-204.8S923.875556 284.444444 807.822222 284.444444z"
            fill="currentColor"
            p-id="31868">
        </path>
    </svg>
</template>

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

const props = defineProps({
  size: { type: [Number, String], default: 20 },
  color: { type: String, default: 'currentColor' },
})

const sizeCss = computed(() => (typeof props.size === 'number' ? `${props.size}px` : props.size))
const color = computed(() => props.color)
</script>

然后在其他组件中使用:

相关推荐
徐小夕1 小时前
万字长文!DeepSeek Super-agent × 企业知识库架构深度拆解与落地实践
前端·vue.js·github
陈随易1 小时前
TanStarter,从搭建到支付,一步到位开始赚美元
前端·后端·程序员
前端双越老师1 小时前
程序员 1v1 访谈系列第十二期:普通人没有核心竞争力
前端·ai编程·全栈
柳杉2 小时前
没有写一行代码:我用 Codex 制作 了一个 3D 智慧展厅可视化大屏
前端·ai编程·three.js
IT_陈寒2 小时前
Redis开发实战:从入门到精通
前端·人工智能·后端
恋猫de小郭2 小时前
Flutter Impeller Vulkan 内存持续上涨,问题没想到是文字阴影
android·前端·flutter
0xBADCODE2 小时前
2026 强网杯 Web 题 cwp · 全链路复盘
前端·安全·web安全·网络安全·系统安全·ctf
麦麦麦当劳大王11 小时前
【油猴脚本】Boss直聘 岗位职责复制
开发语言·前端·javascript
小lu飞11 小时前
2026小红书小工具怎么制作:纯前端 ZIP 打包、沙箱限制与笔记挂载流程
前端·笔记·ai·ai编程
smallcelebration12 小时前
151 从0开始搭建后端项目
前端·firefox