在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>

然后在其他组件中使用:

相关推荐
胡萝卜术7 分钟前
从暴力到滑动窗口的终极形态:力扣3「无重复字符的最长子串」的优化进化之路
前端·javascript·面试
钛态9 小时前
前端安全防线:CSRF 攻击链路与双重 Token 校验的工程实现
前端·vue·react·web
张拭心10 小时前
技术管理笔记:让我“燃尽”的一天
前端
不好听61311 小时前
BFF 架构实战:从前端直调 API 到加入中间层
前端·架构
不好听61311 小时前
前端跨域完全指南:从为什么报错到三种解决方案
前端
三84411 小时前
路由策略/控制 配置双点双向路由重发布
服务器·前端·javascript
Qimooidea11 小时前
祁木 CAD Translator 工程图纸出海实战指南
服务器·前端·安全
小林ixn11 小时前
从BFF到SSE:我在Vue项目里藏了个“AI翻译官”
前端·vue.js·vite
元气少女小圆丶13 小时前
unity发布web嵌入到前端页面的接受参数
前端·unity·webgl
工业HMI实战笔记13 小时前
【拯救HMI】:低碳制造:自动化技术如何助力企业节能降耗
运维·前端·自动化·交互·制造