svg图标使用

自定义svg组件

<template>
  <svg :class="svgClass" aria-hidden="true" v-on="$listeners" :style="{opacity:opacity,fill:fill,width:width+'px',height:height+'px'}">
    <use :xlink:href="iconName" />
  </svg>
</template>
 
<script>
  export default {
    name: 'SvgIcon',
    props: {
      iconClass: {
        type: String,
        required: true
      },
      className: {
        type: String,
        default: 'home_index'
      },
      opacity:{
        type:Number,
      },
      fill:{
        type:String,
        default:''
      },
      width:{
        type:Number,
        default:14
      },
      height:{
        type:Number,
        default:14
      }
    },
    computed: {
      iconName() {
        return `#icon-${this.iconClass}`
      },
      svgClass() {
        if (this.className) {
          return 'svg-icon ' + this.className
        } else {
          return 'svg-icon'
        }
      }
    }
  }
</script>
 
<style scoped>
  .svg-icon {
    vertical-align: -1px;
    overflow: hidden;
  }
</style>

全局注入自定义组件

import Vue from 'vue'
import { XcIcon } from '@/components/index' // svg组件

// register globally
Vue.component('xc-icon', XcIcon)

const req = require.context('./svg', false, /\.svg$/) // 引入svg文件
const requireAll = requireContext => requireContext.keys().map(requireContext)
requireAll(req)

使用自定义组件

<div class="table">
<xc-icon iconClass="add_icon" class="edit-in-table"></xc-icon>
</div>
<style>
.table {
	.edit-in-table {
		  fill: #9EA7B4;
          margin-left: -2px;
	}
}
.table:hover {
	.edit-in-table {
        color: #1F6CDD;
        fill: #1F6CDD;
	}
}
</style>

特别注意

引入svg图标的时候,需确定下fill是否已经填充颜色,如果已经填充颜色需要删除

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值