前端开发必备: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'}"/>
相关推荐
小猪皮蛋粥3 分钟前
python画图
开发语言·python
Felven3 分钟前
A. The 67th Integer Problem
开发语言
zopple6 分钟前
Laravel7.x核心特性全解析
开发语言·php·laravel
wjs20246 分钟前
MVC 应用程序
开发语言
lly2024067 分钟前
ionic 模态窗口:全面解析与最佳实践
开发语言
小白学大数据9 分钟前
解决 Python 爬虫被限制:延迟抓取指令深度解析
开发语言·c++·爬虫·python
ZC跨境爬虫11 分钟前
3D 地球卫星轨道可视化平台开发 Day6(SEC数据接口扩展实现)
前端·microsoft·3d·html·json·交互
qq_120840937119 分钟前
Three.js 工程向:EffectComposer 后处理链路与色彩管理
开发语言·前端·javascript
|晴 天|21 分钟前
评论系统与情感分析
前端·ai·typescript
沉默中爆发的IT男27 分钟前
BGP基础配置实验总结
linux·服务器·前端