一、创建IconSvg组件

IconSvg.vue
<template>
<svg class="svg-icon" aria-hidden="true">
<use :xlink:href="iconName"></use>
</svg>
</template>
<script>
export default {
name: "IconSvg",
props: {
iconClass: {
type: String,
required: true
}
},
computed: {
iconName() {
return `#${this.iconClass}`
}
}
}
</script>
<style scoped>
.svg-icon {
width: 1em;
height: 1em;
font-size: 18px;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
</style>
二、登录iconfont官网,复制Symbol生成的.js

三、复制Symbol生成的.js代码放在index.html里

四、引用
在需要引用icon-svg组件的地方

就可以直接引用了
<icon-svg icon-class="icon-check-circle-fill" class="icon_svg" style="{color: '#42B782'}"/>
其中,icon-class是如下图中,该组件在IconFont中的Font Class
注意:有些图标前缀还需要加上icon-,如当前的这个图标例子,就需要加上icon-

以上就是局部安装icon的方法。
附:以下是全局安装icon组件
全局安装icon组件后,就不用每个页面都import以及注册component了
一、创建如下文件夹目录

其中IconSvg.vue跟局部安装文件一样,无需更改。
二、index.js
import IconSvg from './IconSvg'
// 添加install方法
IconSvg.install = function(Vue) {
Vue.component(IconSvg.name, IconSvg);
};
export default IconSvg;
三、main.js

6万+



被折叠的 条评论
为什么被折叠?



