vue中手动封装iconfont组件解析(三种引用方式的封装和使用)

 更新时间:2022年09月08日 11:00:05   作者:艾欢欢  
这篇文章主要介绍了vue中手动封装iconfont组件(三种引用方式的封装和使用),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

在线使用 有时候会因网络问题影响用户体验;直接放在 本地使用 ,如果过多使用也会显得繁琐,所以就可以将其封装成一个组件,也方便维护。​

封装基于阿里巴巴图标库的项目图标。

准备

将项目内的图标下载至本地

img

在了路径 src/assets 下新建文件夹 iconfont ,用来存放字体图标的本地文件

解压下载到本地的字体图标文件,放到 iconfont 文件夹下

如过项目中没有下载 css-loader 依赖包,就进行下载,否则会报错

npm install css-loader -D

封装

unicode引用封装

<template>
  <div>
    <span class="iconfont" v-html="name"></span>
    <slot></slot>
  </div>
</template>
  
 
<script>
export default {
  name: 'iconUnicode',
  props: {
    name: {
      type: String,
      required: true
    }
  }
}
</script>
<style scoped>
@font-face {
  /* Unicode  */
  font-family: "iconfont";
  src: url("../assets/iconfont/iconfont.eot");
  src: url("../assets/iconfont/iconfont.eot?#iefix") format("embedded-opentype"),
    url("../assets/iconfont/iconfont.woff2") format("woff2"),
    url("../assets/iconfont/iconfont.woff") format("woff"),
    url("../assets/iconfont/iconfont.ttf") format("truetype"),
    url("../assets/iconfont/iconfont.svg#iconfont") format("svg");
}
.iconfont {
  font-family: "iconfont" !important;
  font-size: 2em;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
</style>

font-class引用封装

<template>
  <div>
    <span class="iconfont" :class="iconTag"></span>
    <slot></slot>
  </div>
</template>

   

<script>
import "../assets/iconfont/iconfont.css";
export default {
  name: "iconFontClass",
  props: {
    name: {
      type: String,
      required: true
    }
  },
  computed: {
    iconTag() {
      return `icon-${this.name}`;
    }
  }
};
</script>
<style scoped>
.iconfont {
  font-family: "iconfont" !important;
  font-size: 2em;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
</style>

symbol引用封装

<template>
  <div>
    <svg class="icon" aria-hidden="true">
      <use :xlink:href="iconTag" rel="external nofollow" ></use>
    </svg>
    <slot></slot>
  </div>
</template>

   

<script>
import "../assets/iconfont/iconfont.js";
export default {
  name: "iconSymbol",
  props: {
    name: {
      type: String,
      required: true
    }
  },
  computed: {
    iconTag() {
      return `#icon-${this.name}`;
    }
  }
};
</script>
<style scoped>
.icon {
  width: 2em;
  height: 2em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>

引入

全局引入

// main.js
// 引入并注册全局组件
import iconUnicode from './ui/iconUnicode'
Vue.component('iUnicode', iconUnicode)

局部引入

// 局部引入并使用
import iSymbol from "../ui/iconSymbol"
import iFont from "../ui/iconFontClass"
export default {
   //注册
  components: {
    iSymbol,
    iFont
  }
};

使用

<template>
  <div class="box">
    <i-symbol name="fanhuidingbu">Symbol</i-symbol>
    <i-font name="fanhuidingbu">Font class</i-font>
    <i-unicode name="&#xe633;" style="font-size:30px;color:#333">Unicode</i-unicode>
  </div>
</template>

效果图:

最后

也可以通过在线链接进行封装,但不管是在线使用还是本地使用,每次在项目中添加新图标之后都要更新一下 本地iconfont文件 或者 在线链接 。

demo 已上传 GitHub

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

相关文章

  • Nuxt.js之自动路由原理的实现方法

    Nuxt.js之自动路由原理的实现方法

    这篇文章主要介绍了Nuxt.js之自动路由原理的实现方法,nuxt.js会根据pages目录结构自动生成vue-router模块的路由配置。非常具有实用价值,需要的朋友可以参考下
    2018-11-11
  • vue watch报错:Error in callback for watcher "xxx":"TypeError的解决方法

    vue watch报错:Error in callback for watcher "xxx&qu

    这篇文章主要给大家介绍了关于vue watch报错:Error in callback for watcher “xxx“:“TypeError:Cannot read properties of undefined的解决方法,需要的朋友可以参考下
    2023-03-03
  • vue 指令版富文本溢出省略截取示例详解

    vue 指令版富文本溢出省略截取示例详解

    这篇文章主要为大家介绍了vue 指令版富文本溢出省略截取示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Vue中$attrs和$listeners详解以及使用方法

    Vue中$attrs和$listeners详解以及使用方法

    最近在研究Vue的组件库,之前也用过$attrs和$listeners,官方文档描述的不太详细,也没有太好的例子,下面这篇文章主要给大家介绍了关于Vue中$attrs和$listeners详解以及使用的相关资料,需要的朋友可以参考下
    2022-11-11
  • vue使用GraphVis开发无限拓展的关系图谱的实现

    vue使用GraphVis开发无限拓展的关系图谱的实现

    本文主要介绍了vue使用GraphVis开发无限拓展的关系图谱,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • Vue3开发必备的六个VSCode插件推荐

    Vue3开发必备的六个VSCode插件推荐

    在VSCode中添加好用的插件可以提高我们的开发效率,这些可以帮助我们格式化,扩充性,执行最佳实践的代码方式,自动完成一些琐碎的事情,下面这篇文章主要给大家推荐介绍了关于Vue3开发必备的六个VSCode插件,需要的朋友可以参考下
    2022-12-12
  • 详解vue中v-model和v-bind绑定数据的异同

    详解vue中v-model和v-bind绑定数据的异同

    这篇文章主要介绍了vue中v-model和v-bind绑定数据的异同,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • vue实现弹窗拖拽效果

    vue实现弹窗拖拽效果

    这篇文章主要为大家详细介绍了vue实现弹窗拖拽效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • 使用Vue实现瀑布流的示例代码

    使用Vue实现瀑布流的示例代码

    这篇文章主要为大家详细介绍了如何使用Vue实现瀑布流,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • vue3中使用swiper的完整版教程(超详细!)

    vue3中使用swiper的完整版教程(超详细!)

    工作中日常笔记,vue中使用swiper插件,在pc端和h5端也是常用的插件,下面这篇文章主要给大家介绍了关于vue3中使用swiper的完整版教程,需要的朋友可以参考下
    2023-04-04

最新评论