Front-end——vue封装图标库

如果element中的图标不够用时,就需要自己准备图标,下面就结合 iconfont(阿里矢量库)封装图标

方法1:使用图标对应的svg代码

在element-ui标签中,加入svg代码,就可以使用

效果:

方法2:下载使用

(1)进入项目目录,引入svg

(2)下载svg资源,统一存储,将其导入,以组件形式使用

import shops from '@/components/icons/svg/shops.svg?inline';

效果展示:

法2麻烦在,每个图标需要使用时都需要import一次

3、封装图标库

定义一个模块,让其根据所选图片修改图标(SVG),动态传入的使用 props传数据(父传子)

  • 修改图标的颜色使用 fill 属性
  • 模块的导入可以收用便捷方法: () => import("./" + this.name + ".svg?inline")
<template>
	<!-- component 代表需要引一个组件 (不确定引哪一个)-->
	<component :is="hansvg" 
	:style="{width: width + 'px',height: height + 'px'}"
	:fill='color'>
	</component>
</template>

<script>
	import shops from './shops.svg?inline'
	export default{
		name: 'wy_icon',
		props:{
			name: {
				type: String
			},
			width:{
				type: String,
				default: '100'
			},
			height:{
				type: String,																																																																			
				default: '100' //设置一个默认值
			},
			color:{
				type: String,
				default: '#0000ff'
			}
		},
		data(){
			return {
				shops: shops
			}
		},
		created(){
			console.log(this.name)
		},
		computed:{
            //修改图标
			hansvg() {
				return () => import("./" + this.name + ".svg?inline")
			}
		},
		methods:{
			
		}
	}
</script>

<style>
</style>

(1)图标全局注册

需要在main.js中进行全局注册,之后就可以在其他组件中直接使用了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Star星屹程序设计

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值