前端开发必备: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'}"/>
相关推荐
编程轨迹_20 分钟前
使用 Spring 和 Redis 创建处理敏感数据的服务
java·开发语言·restful
赵和范31 分钟前
C++:书架
开发语言·c++·算法
CodeCraft Studio1 小时前
报表控件stimulsoft教程:使用 JoinType 关系参数创建仪表盘
前端·ui
boooo_hhh1 小时前
第J7周:对于ResNeXt-50算法的思考
开发语言·python·深度学习
春天姐姐2 小时前
vue知识点总结 依赖注入 动态组件 异步加载
前端·javascript·vue.js
LSL666_2 小时前
Java——多态
java·开发语言·多态·内存图
麓殇⊙2 小时前
CurrentHashMap的整体系统介绍及Java内存模型(JVM)介绍
java·开发语言·jvm
互联网搬砖老肖2 小时前
Web 架构之数据读写分离
前端·架构·web
LSL666_3 小时前
Java——包装类
java·开发语言·包装类
yasuniko3 小时前
C++线程库
开发语言·c++