前端开发必备: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'}"/>
相关推荐
weixin_443478513 分钟前
flutter组件学习之对话框与提示详解
javascript·学习·flutter
lightqjx15 分钟前
【前端】前端学习一之HTML从入门到精通
前端·学习·html
Joyee69115 分钟前
RN 的事件调度 RuntimeScheduler
前端·react native
LilySesy20 分钟前
【与AI+】英语day1——ABAP基础与数据类型
开发语言·ai·sap·abap
你不是我我23 分钟前
【Java 开发日记】我们来说一下 b+ 树与 b 树的区别
java·开发语言
2501_9249526930 分钟前
C++中的过滤器模式
开发语言·c++·算法
左左右右左右摇晃30 分钟前
Java笔记——IO
java·开发语言·笔记
2401_8732046531 分钟前
C++中的组合模式实战
开发语言·c++·算法
twc82932 分钟前
Query 改写 大模型测试的数据倍增器
开发语言·人工智能·python·rag·大模型测试
Byron__34 分钟前
HashSet/LinkedHashSet/TreeSet 原理深度解析
java·开发语言