前端开发必备:icon封装技巧

main.js
复制代码
import svgIcon from '@/components/SvgIcon'
Vue.component('svg-icon-full', svgIcon)
const requireAll = requireContext => requireContext.keys().map(requireContext)
const req = require.context('@/assets/icons/svg', false, /\.svg$/)  // icon位置
requireAll(req)
SvgIcon组件
复制代码
<template>
  <i v-if="iconFileName.indexOf('el-icon-') === 0" :class="iconFileName" />
  <svg v-else class="svg-icon" aria-hidden="true" v-on="$listeners">
    <use :xlink:href="`#icon-${iconFileName}`" rel="external nofollow" :class="[iconColor]"/>
  </svg>
</template>

<script>
export default {
  name: 'SvgIcon',
  props: {
    iconFileName: {
      type: String,
      required: true
    },
    iconColor: {
      type: String,
      default: ''
    }
  }
}
</script>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  overflow: hidden;
  vertical-align: -0.15em;
  fill: currentColor;
}
</style>
SvgIcon调用

icon-file-name="文件名"

复制代码
<svg-icon-full icon-file-name="alarm" :style="{width: '14px', height: '14px', color: '#FF4621'}"/>
相关推荐
Hilaku12 分钟前
做中后台业务,为什么我不建议你用 Tailwind CSS?
前端·css·代码规范
lbb 小魔仙13 分钟前
Python_PromptEngineering终极指南
开发语言·python
有意义13 分钟前
【面试复盘】前端底层原理与 React 核心机制深度梳理
前端·react.js·面试
二十一_14 分钟前
LangChain 教程 05|模型配置:AI 的大脑与推理引擎
前端·langchain
阿Y加油吧17 分钟前
单调栈与堆的经典应用:柱状图最大矩形 & 数组第 K 大元素深度解析
开发语言·python
kerli17 分钟前
Compose 组件:BoxWithConstraints作用及其原理
android·前端
Strugglingler18 分钟前
Python学习记录
开发语言·python
LovroMance19 分钟前
消息总线 + 可插拔的消息插件管理系统
前端
Lee川19 分钟前
React Router 实战指南:构建现代化前端路由系统
前端·react.js·架构
薛纪克21 分钟前
前端自动化测试的 Spec 模式:用 Kiro Power 实现从需求到脚本的全链路自动化
前端·自动化测试·ai·kiro