vue3 + element plus使用iconfont 自定义font组件&颜色大小可修改

vue3 + element plus使用iconfont 自定义font组件&颜色大小可修改这里写自定义目录标题


参考网上方案新建SvgIcon.vue,但没说明怎么修改颜色及大小,我在这个博客中简单提供下。

自定义SvgIcon.vue

在componet目录下新建SvgIcon.vue组件

 // src/components/SvgIcon.vue
<template>
  <svg :class="svgClass" aria-hidden="true">
    <use :fill="color" :xlink:href="iconClassName"/>
  </svg>
</template>

<script lang="ts" setup>
import {computed} from 'vue';

const props = defineProps({
  iconName: {
    type: String,
    required: true
  },
  className: {
    type: String,
    default: ''
  },
  color: {
    type: String,
    default: '#409eff'
  }
});
// 图标在 iconfont 中的名字
const iconClassName = computed(() => {
  return `#${props.iconName}`;
});
// 给图标添加上类名
const svgClass = computed(() => {
  if (props.className) {
    return `svg-icon ${props.className}`;
  }
  return 'svg-icon';
});
// 给图标添加上颜色
const color = computed(() => {
  return props.color;
})
</script>

<script lang="ts">
export default {
  name: 'svgIcon'
};
</script>

<style lang="less" scoped>
.svg-icon {
  position: relative;
  width: 1em;
  height: 1em;
  vertical-align: -2px;
  fill: currentColor;
}
</style>


引入iconfont

在asserts文件夹中新建一个iconfont文件夹,并在该文件夹下新建一个iconfont.js,把iconfont复制下来即可
  
在这里插入图片描述

App.vue中引入组件

import SvgIcon from '@/components/iconfont/SvgIcon.vue';
import '@/assets/iconfont/iconfont.js';
import ElementPlus from 'element-plus';
// use
app.component('SvgIcon', SvgIcon).use(ElementPlus)

更改图标大小

使用el-icon即可,也可以在SvgIcon.vue中新建

<el-icon :size="14" class="icon">
   <SvgIcon iconName="icon-xinjian" color="#fff"/>
 </el-icon>
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值